Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Game.asm
- *
- * Created: 12/14/2018 5:10:02 PM
- * Author: amore448
- */
- .org $2A
- .def zeroreg = r1
- .equ VMEM_SZ = 5 ; #rows on display
- .equ AD_CHAN_X = 6 ; ADC6=PA6, PORTA bit 6, X-led
- .equ AD_CHAN_Y = 7 ; ADC7=PA7, PORTA bit 7, Y-led
- .equ GAME_SPEED = 70 ; inter-run delay (millisecs)
- .equ PRESCALE = 7 ; AD_prescalar value
- .equ BEEP_PITCH = 5 ; Victory beep pitch
- .equ BEEP_LENGTH = 150 ; Victory beep length
- ; --------------------------------------------------------------------
- ; --- Memory layout in SRAM
- .dseg
- .org SRAM_START
- POSX: .byte 1
- POSY: .byte 1
- TPOSX: .byte 1
- TPOSY: .byte 1
- LINE: .byte 1
- VMEM: .byte VMEM_SZ
- SEED: .byte 1
- ; --------------------------------------------------------------------
- ; --- Macros for inc/dec-rementing
- ; --- a byte in SRAM
- .macro INCSRAM ; inc byte in SRAM
- lds r19,@0
- inc r19
- sts @0,r19
- .endmacro
- .macro DECSRAM ;dec byte in SRAM
- lds r19,@0
- dec r19
- sts @0,r19
- .endmacro
- ; --------------------------------------------------------------------
- ; --- Code
- //.cseg
- .org $00
- rjmp START
- .org INT0addr
- rjmp MUX
- START:
- clr zeroreg
- ldi r16,HIGH(RAMEND)
- out SPH,r16
- ldi r16,LOW(RAMEND)
- out SPL,r16
- call HW_INIT
- call WARM
- RUN:
- call JOYSTICK
- call ERASE
- call UPDATE
- call DELAY_LOOP
- ldi ZH,HIGH(POSX)
- ldi ZL,LOW(POSX)
- ldi r18,2
- HIT:
- ld r16,Z
- ldd r17,Z+2
- cp r16,r17
- brne NO_HIT
- inc ZL
- dec r18
- brne HIT
- ldi r18,BEEP_LENGTH
- BEEP_LENGTH_LOOP:
- call BEEP
- dec r18
- brne BEEP_LENGTH_LOOP
- HIT_END:
- call WARM
- NO_HIT:
- rjmp RUN
- MUX:
- push r16
- in r16,SREG
- push r16
- push r19
- push ZH
- push ZL
- ldi ZH,HIGH(VMEM)
- ldi ZL,LOW(VMEM)
- INCSRAM LINE
- lds r16,LINE
- cpi r16,5
- brne MUX_END
- ldi r16,0
- sts LINE,r16
- MUX_END:
- out PORTB,zeroreg
- INCSRAM SEED
- out PORTA,r16
- add ZL,r16
- ld r16,Z
- out PORTB,r16
- pop ZL
- pop ZH
- pop r19
- pop r16
- out SREG,r16
- pop r16
- reti
- ; --------------------------------------------------------------------
- ; --- Converts Analog to digital and changes position accordingly
- ; --- Uses: r16
- JOYSTICK:
- ;-------------------------------- Convert x --------------------------------
- cbi ADMUX,0
- call AD_GET
- cpi r16,$03
- brne INCREASE_X
- DECSRAM POSX
- INCREASE_X:
- cpi r16,$00
- brne CONVERT_Y
- INCSRAM POSX
- ;-------------------------------- Convert y --------------------------------
- CONVERT_Y:
- sbi ADMUX,MUX0
- call AD_GET
- cpi r16,$03
- brne DECREASE_Y
- INCSRAM POSY
- DECREASE_Y:
- cpi r16,$00
- brne JOY_LIM
- DECSRAM POSY
- JOY_LIM:
- call LIMITS
- ret
- AD_GET:
- /*push r17
- in r17,ADMUX
- andi r17,$f0
- or r17,r16
- out ADMUX,r17*/
- sbi ADCSRA,ADSC
- WAIT: sbic ADCSRA,ADSC
- rjmp WAIT
- in r16,ADCH
- pop r17
- ret
- ; --------------------------------------------------------------------
- ; --- LIMITS Limit POSX, POSY coordinates
- ; --- Uses: r16,r17
- LIMITS:
- lds r16,POSX ; variable
- ldi r17,7 ; upper limit + 1
- call POS_LIM ; actual work
- sts POSX,r16
- lds r16,POSY
- ldi r17,5
- call POS_LIM
- sts POSY,r16
- ret
- POS_LIM:
- ori r16,0 ; negative?
- brmi POS_LESS ; POSX neg => add 1
- cp r16,r17 ; past edge
- brne POS_OK
- subi r16,2
- POS_LESS:
- inc r16
- POS_OK:
- ret
- ; --------------------------------------------------------------------
- ; --- UPDATE VMEM
- ; --- with POSX/Y, TPOSX/Y
- ; --- Uses: r16, r17, Z
- UPDATE:
- clr ZH
- ldi ZL,LOW(POSX)
- call SETPOS
- clr ZH
- ldi ZL,LOW(TPOSX)
- call SETPOS
- ret
- ; --- SETPOS Set bit pattern of r16 into *Z
- ; --- Uses: r16, r17, Z
- ; --- 1st call Z points to POSX at entry and POSY at exit
- ; --- 2nd call Z points to TPOSX at entry and TPOSY at exit
- SETPOS:
- ld r17,Z+ ; r17=POSX
- call SETBIT ; r16=bitpattern for VMEM+POSY
- ld r17,Z ; r17=POSY Z to POSY
- ldi ZL,LOW(VMEM)
- add ZL,r17 ; Z=VMEM+POSY, ZL=VMEM+0..4
- ld r17,Z ; current line in VMEM
- or r17,r16 ; OR on place
- st Z,r17 ; put back into VMEM
- ret
- ; --- SETBIT Set bit r17 on r16
- ; --- Uses: r16, r17
- SETBIT:
- ldi r16,$01 ; bit to shift
- SETBIT_LOOP:
- dec r17
- brmi SETBIT_END
- lsl r16
- jmp SETBIT_LOOP
- SETBIT_END:
- ret
- ; --------------------------------------------------------------------
- ; --- Hardware init
- ; --- Uses: r16
- HW_INIT:
- ldi r16,$5f
- out DDRA,r16
- ldi r16,$3f
- out DDRB,r16
- ldi r16,(1<<REFS0)|(1<<REFS1)|(0<<ADLAR)|(1<<MUX2)|(1<<MUX1)
- out ADMUX,r16
- ldi r16,(1<<ADEN)|(1<<ADPS1)
- out ADCSRA,r16
- ldi r16,(1<<ISC01)|(1<<ISC00)
- out MCUCR,r16
- ldi r16,(1<<INT0)
- out GICR,r16
- sei
- ret
- WARM:
- ldi r16,$00
- sts POSX,r16
- ldi r16,$02
- sts POSY,r16
- push r0
- push r0
- in r16,SPH
- mov ZH,r16
- in r16,SPL
- inc r16
- mov ZL,r16
- call RANDOM
- pop r0
- sts TPOSY,r0
- pop r0
- sts TPOSX,r0
- call ERASE
- ret
- RANDOM:
- ldi r17,2
- lds r16,SEED
- GET_RANDOMY:
- swap r16
- mov r18,r16
- andi r18,$07 ; get the last 3 bits in SEED r16 <= 7
- cpi r18,5
- brmi GET_RANDOMX
- subi r18,4
- GET_RANDOMX:
- cpi r17,1
- brne RANDOM_END
- subi r18,-2
- RANDOM_END:
- st Z+,r18
- dec r17
- brne GET_RANDOMY
- ret
- ERASE:
- ldi ZH,HIGH(VMEM)
- ldi ZL,LOW(VMEM)
- ldi r18,5
- SET_VMEM:
- st Z+,zeroreg
- dec r18
- brne SET_VMEM
- ERASE_END:
- ret
- ; --------------------------------------------------------------------
- ; --- Delay 1 ms
- ; --- Uses: r16, r17
- DELAY:
- ldi r16,10 ;Decimal bas
- delayYttreLoop:
- ldi r17,$1f
- delayInreLoop:
- dec r17
- brne delayInreLoop
- dec r16
- brne delayYttreLoop
- ret
- ; --------------------------------------------------------------------
- ; --- Delay 1 ms
- ; --- Uses: r18
- DELAY_LOOP:
- ldi r18,GAME_SPEED
- DELAY_LOOPX:
- call DELAY
- dec r18
- brne DELAY_LOOPX
- ret
- BEEP:
- sbi PORTA,3
- call BEEP_DELAY
- cbi PORTA,3
- call BEEP_DELAY
- ret
- BEEP_DELAY:
- ldi r16,BEEP_PITCH
- delayYttreBeepLoop:
- ldi r17,$1f
- delayInreBeepLoop:
- dec r17
- brne delayInreBeepLoop
- dec r16
- brne delayYttreBeepLoop
- ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement