;**** includes ****  
.include "m8def.inc" 
.macro	outi 
	ldi	r16,@1 
	out	@0,r16 
.endm
.macro  ldiw	;ldiw    Z(X/Y),var
        ldi     @0L,low(@1)
        ldi     @0H,high(@1)
.endm
.MACRO cpi16	;cpi16	ra,rb,0xHHHH	;ra:rb-0xHHHH	set flags:N,C,Z
		push	@0
		push	@1
		subi	@1,low(@2)
		sbci	@0,high(@2)
		pop	@1
		pop	@0
.ENDMACRO

;***************************** 
;* レジスタ変数　設定 * 
;*****************************
.def	trig_s 	= r10
.def	trig_a 	= r11
.def	sf	= r12
.def	trig_b  = r13
.def	sreg_bak= r14
.def	rgb_dat	= r15
.def	temp1	= r16 
.def	temp2	= r17 
.def	temp3	= r18
.def	temp4	= r19
.def	c	= r20 
.def	c1	= r21
.def	trig	= r22
.def	dl	= r24
.def	dh	= r25	
 ;***************************** 
;*　 ビット　設定　　* 
;***************************** 
 ;**  PORTB  *** 
.equ	VS	= 4 
.equ	HS 	= 5 
;.equ	dck 	= 1 

.equ	dckon = 0b00011001	;ckt1=ck 
.equ	dckoff= 0b00011000
.equ	black = 0
.equ	white = 255
.equ	blue  = 0b11100000
.equ	red   = 0b00000011
.dseg 
	data:	.BYTE   320
	ad:	.BYTE   302
	m_dat:	.BYTE	60
	m_10:	.BYTE	4*2
	 m_5:	.BYTE	4*2
	 m_2:	.BYTE	4*2
	 m_1:	.BYTE	4*2	
.cseg 

	rjmp	start ;各種ﾘｾｯﾄ
	reti	;RJMP EXT_INT0	;外部割り込み要求0
	reti	;EXT_INT1 	;外部割り込み要求1
	rjmp	TIM2_COMP 	;ﾀｲﾏ/ｶｳﾝﾀ2 比較一致
  
;********************* 
;* メイン プログラム * 
;********************* 
start: 
	ldi	temp1,low(RAMEND) 
	out	SPL,temp1 
	ldi	temp1,high(RAMEND) 
	out	SPH,temp1	;init Stack Pointer  
	 
	ldi	temp1,0xff 
	out	DDRB,temp1		; PB0~7 out port 
	outi	PORTB,0b00110000	; HS,VS="H" 
	ldi	temp1,0xff 
	out	DDRD,temp1	; PD0~7 out port 
	ldi	temp1,0x00	; PD0~7 clear	 
	out	PORTD,temp1 
 	ldi	temp1,0x00 
	out	DDRC,temp1	; PC0~7 in port 
	ldi	temp1,0xff	; PC0~7 pull up	 
	out	PORTC,temp1 
ADC_int:
	outi	ADMUX,(1<<REFS0)|(1<<ADLAR)		;基準電圧VCC、結果左揃え、Ch0選択
	outi	ADCSR,(1<<ADEN)|(1<<ADPS2)|(1<<ADPS1);|(1<<ADPS0)	;ADC on,ADCck=16M/64=250k
time2_int:
	ldi	temp1,25
	mov	sf,temp1	;比較値=200,250/25=10kHz
		
	outi	TIMSK,1<<OCIE2		;比較一致割込使用
	outi	TCNT2,0			;カウンタクリア
time1_int:
	outi	OCR1AH,0x00		;Fpmw=ck/6
	outi	OCR1AL,0x04		;
	outi	ICR1H,0x00		;
	outi	ICR1L,0x05
	outi	TCCR1A,0b11000010	;OC1A反転、高速PWM Mode　TOP=ICR1 
	outi	TCCR1B,dckon  
   	ldi	temp1,32
	mov	trig_a,temp1
	ldi	trig,0
add_set:
	ldiw	x,m_10
	ldiw	z,dat_sp*2
	st	x+,ZL
	st	x+,ZH
	st	x+,ZL;sp
	st	x+,ZH
	ldiw	z,dat_1*2
	st	x+,ZL
	st	x+,ZH
	ldiw	z,dat_0*2
	st	x+,ZL
	st	x+,ZH
	
	ldiw	x,m_5
	ldiw	z,dat_sp*2
	st	x+,ZL
	st	x+,ZH
	st	x+,ZL;sp
	st	x+,ZH
	st	x+,ZL;sp
	st	x+,ZH
	ldiw	z,dat_5*2
	st	x+,ZL
	st	x+,ZH
	
	ldiw	x,m_2
	ldiw	z,dat_sp*2
	st	x+,ZL
	st	x+,ZH
	ldiw	z,dat_2*2
	st	x+,ZL
	st	x+,ZH
	ldiw	z,dat_dt*2
	st	x+,ZL
	st	x+,ZH
	ldiw	z,dat_5*2
	st	x+,ZL
	st	x+,ZH

	ldiw	x,m_1
	ldiw	z,dat_1*2
	st	x+,ZL
	st	x+,ZH
	ldiw	z,dat_dt*2
	st	x+,ZL
	st	x+,ZH
	ldiw	z,dat_2*2
	st	x+,ZL
	st	x+,ZH
	ldiw	z,dat_5*2
	st	x+,ZL
	st	x+,ZH
loop:
	sbis	PINC,4
	rjmp	loop
	clr trig_s
	ldi	temp1,100
	rcall wait_ms

loop0:
	sbic	PINC,1
	rjmp	loop_0
	ldi	temp1,7
	rcall wait_ms
	sbic	PINC,1
	rjmp	loop_0
	add trig,trig_a				 
	brcc	loop_0
	clr	trig
loop_0:
	sbic	PINC,2
	rjmp	loop_0a
	ldi	temp1,7
	rcall wait_ms
	sbic	PINC,2
	rjmp	loop_0a
	lsl	sf	
	brcc	loop_0a
	ldi	temp1,25
	mov 	sf,temp1

loop_0a:
	ldiw	y,m_dat
	ldiw	z,dat_S*2
	ldi	c,18
loop_0b:
	lpm	temp1,z+
	st	y+,temp1	;'Sf:'
	dec	c
	brne	loop_0b
	
	mov	temp2,sf
	cpi	temp2,25
	brne	loop_0c
	ldiw	x,m_10
	rjmp	loop_0h
loop_0c:
	cpi	temp2,50
	brne	loop_0d
	ldiw	x,m_5
	rjmp	loop_0h
loop_0d:
	cpi	temp2,100
	brne	loop_0e
	ldiw	x,m_2
	rjmp	loop_0h
loop_0e:
	ldiw	x,m_1
loop_0h:
	ldi	c,4
loop_0f:
	ld	zl,x+
	ld	zh,x+
	ldi	c1,6
loop_0i:
	lpm	temp1,z+
	st	y+,temp1
	dec	c1
	brne	loop_0i
	dec	c
	brne	loop_0f

	ldiw	z,dat_k*2
	ldi	c,18
loop_0j:
	lpm	temp1,z+
	st	y+,temp1	;'kHz'
	dec	c
	brne	loop_0j


loop_1:
	ldiw	d,0
	ldiw	x,ad
	out	OCR2,sf
	outi	TCCR2,(1<<CS21)|(1<<CS20)|(1<<WGM21)	;タイマ２on　CTC動作、CKT2=16M/64
	sei

data_load:
	tst	dh
	brmi	lcd_dsp
	sbic	PINC,4
	rjmp	data_load
	outi	TCCR2,0	;カウンタ2停止
	clr	trig
	rjmp	loop
lcd_dsp:
	rcall	lcd_ctrl 
	rjmp	loop
 

TIM2_COMP:
	in	sreg_bak,SREG
	sbi     ADCSR,ADIF	; Reset interrupt flag
 	sbi	ADCSR,ADSC      ; Start A/D conversion
ready:	
 	sbis	ADCSR,ADIF
 	rjmp	ready
	in	temp1,ADCL	;xL------
	in	temp2,ADCH	;Mxxxxxxx
	mov	temp3,trig_b
	mov 	trig_b,temp2
	tst	trig_s
	brne	comp_1
	tst	trig
	breq	comp_0

	cp	temp3,temp2
	brpl	COMP_E	
	cp	temp2,trig
	brne	COMP_E
comp_0:
	inc 	trig_s
comp_1:
	lsr	temp2		;1024--->200
	ror	temp1		;1/2
	mov	temp4,temp2
	mov	temp3,temp1		
	lsr	temp2
	ror	temp1		;1/2*1/2	
	add	temp3,temp1
	adc	temp4,temp2	;1/2+1/4
	lsr	temp2
	ror	temp1		
	lsr	temp2
	ror	temp1
	lsr	temp2
	ror	temp1		;1/2*1/2*1/8
	add	temp3,temp1
	adc	temp4,temp2	;1/2+1/4+1/32=25/32		
	ldi	temp1,55
	lsl	temp3		;切り上げ
	adc	temp1,temp4	;1/4*25/32=25/128(200/1024) (0-200 --> 55-255)
	st	x+,temp1
	adiw	dl,1
	cpi16	dh,dl,301
	brne	COMP_E
	outi	TCCR2,0	;カウンタ2停止
	ldi	dh,0x80
COMP_E:
	out	SREG,sreg_bak
	reti 
;************ 
lcd_ctrl: 
	cbi	PORTB,VS 
	rcall	line_set 
	rcall	line_set 
	rcall	line_set 
	sbi	PORTB,VS 
	rcall	line_set
	rcall	line_set 
	rcall	line_set 
	rcall	line_set 

	ldi	c,240 
	ldi	temp4,20
lcd_1:
	ldiw	y,data
	ldiw	x,ad
	cpi	c,221
	brcs	lcd_1c
	rjmp	lcd_11
lcd_1c:
	cpi	c,20
	brcc	lcd_1b
	rjmp	lcd_10
lcd_1b:
	ldi	temp1,white
	ldi	c1,10
lcd_1a:
	st	y+,temp1
	dec	c1
	brne	lcd_1a	

	ldi	c1,151
	ldi	temp3,0
lcd_2:
	ld	temp1,x
	inc	temp1
	breq	lcd_3	
	ldi	temp2,white
	rjmp	lcd_4
lcd_3:
	ldi	temp2,red	
lcd_4:
	dec	temp3
	brpl	lcd_41
	ldi	temp3,49
	cpi	temp2,red
	breq 	lcd_40
	ldi	temp2,blue
	rjmp 	lcd_41
lcd_40:
	ldi	temp2,blue|red
lcd_41:
	tst	temp4
	brne 	lcd_42
	cpi	temp2,red
	breq	lcd_410
	ldi	temp2,blue
	rjmp	lcd_42
lcd_410:
	ldi	temp2,blue|red
lcd_42:	
	st	y+,temp2
	st	x+,temp1
	dec	c1
	brne	lcd_2

	ldi	c1,150
lcd_5:
	ld	temp1,x
	inc	temp1
	breq	lcd_6	
	ldi	temp2,white
	rjmp	lcd_7
lcd_6:
	ldi	temp2,red	
lcd_7:
	dec	temp3
	brpl	lcd_71
	ldi	temp3,49	
	cpi	temp2,red
	breq 	lcd_70
	ldi	temp2,blue
	rjmp 	lcd_71
lcd_70:
	ldi	temp2,blue|red
lcd_71:
	tst	temp4
	brne 	lcd_72
	cpi	temp2,red
	breq	lcd_710
	ldi	temp2,blue
	rjmp	lcd_72
lcd_710:
	ldi	temp2,blue|red
lcd_72:
	st	y+,temp2
	st	x+,temp1
	dec	c1
	brne	lcd_5
	
	ldi	temp1,white
	ldi	c1,9
lcd_8:
	st	y+,temp1
	dec	c1
	brne	lcd_8	
	rjmp	lcd_12
lcd_10:
	cpi	c,17
	brcc	lcd_11
	cpi	c,10
	brcs	lcd_11

	ldi	temp1,white
	ldi	c1,10
lcd_10a:
	st	y+,temp1
	dec	c1
	brne	lcd_10a

	ldiw	x,m_dat
	ldi	c1,60
lcd_10b:
	ld	temp1,x
	lsl	temp1
	st	x+,temp1
	brcc	lcd_10c
	ldi	temp1,blue|red
	rjmp	lcd_10d
lcd_10c:
	ldi	temp1,white
lcd_10d:
	st	y+,temp1
	dec	c1
	brne	lcd_10b

	ldi	temp1,white
	ldi	c1,250
lcd_10e:
	st	y+,temp1
	dec	c1
	brne	lcd_10e
	rjmp	lcd_12

lcd_11:
	ldi	temp1,white
	ldi	c1,160
lcd_11a:
	st	y+,temp1
	dec	c1
	brne	lcd_11a	

	ldi	c1,160
lcd_11b:
	st	y+,temp1
	dec	c1
	brne	lcd_11b	

lcd_12:
	rcall	line_set 
	dec 	temp4
	brpl	lcd_13
	ldi	temp4,49
lcd_13:
	dec	c 
	breq	lcd_14
	rjmp	lcd_1 
lcd_14:
	rcall	line_set
	rcall	line_set 
	rcall	line_set 
	rcall	line_set 
	ret

wait_ms:
				;temp1　ms
	ldi	temp2,250   	;64*250=16000clocks

wait_ms_01:
	ldi	temp3,15    	;4*(15+1)=64
wait_ms_02:
	nop			; 1clocks
	dec	temp3		; 1clocks
	brne	wait_ms_02	; 2clocks

	nop			; 1clocks
	dec	temp2		; 1clocks
	brne	wait_ms_01	; 2clocks

	dec	temp1		; 1clocks	
	brne	wait_ms		; 2clocks
	ret
 ;************ 
line_set:

	ldiw	z,data 
	cbi	PORTB,HS 
	ldi	c1,3
line_0:
	jmp	 line_01	;3clocks
line_01:
	dec	c1 	;1
	brne	line_0;2/1
				;(6x3)
	sbi	PORTB,HS	;2 
 	ldi	c1,47 		;1
line_1:
	jmp	 line_11	;3clocks
line_11:
	dec	c1  ;1
	brne	line_1 ;2/1
	nop 	;1
	jmp	 line_12	;3clocks(6x48)
line_12:
	ldi	c1,160		;1
line_2:
	ld	rgb_dat,z+	;2
	out	PORTD,rgb_dat   ;1	
	dec	c1 		;1
	brne	line_2      	;2/1
				;(6x160)
	ldi	c1,160	;1(510)  
line_3:
	ld	rgb_dat,z+	;2
	out	PORTD,rgb_dat   ;1	
	dec	c1		;1
	brne	line_3      	;2/1
				;(6x160)
	ldi	c1,8
line_6:
	jmp	 line_61	;3clocks
line_61:
	dec	c1         	;1
	brne	line_6		;2
	nop           		;(6x8)
 	ret 

;文字パターン
dat_S:	.db	0b01100010,0b10010010,0b10010010,0b10010010,0b10001100,0b00000000

dat_f:	.db	0b00010000,0b01111110,0b10010000,0b10000000,0b01000000,0b00000000

dat_co:	.db	0b00000000,0b01100110,0b01100110,0b00000000,0b00000000,0b00000000

dat_k:	.db	0b11111110,0b00001000,0b00010100,0b00100010,0b00000000,0b00000000

dat_H:	.db	0b11111110,0b00010000,0b00010000,0b00010000,0b11111110,0b00000000

dat_z:	.db	0b00100010,0b00100110,0b00101010,0b00110010,0b00100010,0b00000000

dat_dt:	.db	0b00000000,0b00000110,0b00000110,0b00000000,0b00000000,0b00000000

dat_1:	.db	0b00000000,0b01000010,0b11111110,0b00000010,0b00000000,0b00000000

dat_0:	.db	0b01111100,0b10000010,0b10000010,0b10000010,0b01111100,0b00000000

dat_2:	.db	0b01000010,0b10000110,0b10001010,0b10010010,0b01100010,0b00000000

dat_5:	.db	0b11100100,0b10100010,0b10100010,0b10100010,0b10011100,0b00000000

dat_sp:	.db	0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000000
