Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ; --- lab4_skal . asm
- .equ VMEM_SZ = 5 ; # rows on display
- .equ AD_CHAN_X = 0 ; ADC0 = PA0 , PORTA bit 0 X - led
- .equ AD_CHAN_Y = 1 ; ADC1 = PA1 , PORTA bit 1 Y - led
- .equ GAME_SPEED = 70 ; inter - run delay ( millisecs )
- .equ PRESCALE = 7 ; AD - prescaler value
- .equ BEEP_PITCH = 20 ; Victory beep pitch
- .equ BEEP_LENGTH = 100 ; Victory beep length
- ; ---------------------------------------
- ; --- Memory layout in SRAM
- .dseg
- .org SRAM_START
- POSX: .byte 1 ; Own position
- POSY: .byte 1
- TPOSX: .byte 1 ; Target position
- TPOSY: .byte 1
- LINE: .byte 1 ; Current line
- VMEM: .byte VMEM_SZ ; Video MEMory
- SEED: .byte 1 ; Seed for Random
- ; ---------------------------------------
- ; --- Macros for inc / dec - rementing
- ; --- a byte in SRAM
- .macro INCSRAM ; inc byte in SRAM
- lds r16 , @0
- inc r16
- sts @0 , r16
- .endmacro
- .macro DECSRAM ; dec byte in SRAM
- lds r16 , @0
- dec r16
- sts @0 , r16
- .endmacro
- ; ---------------------------------------
- ; --- Code
- .cseg
- .org $0
- jmp START
- .org INT0addr
- jmp MUX
- START:
- ;*** KLAR ***
- ;*** Sätt stackpekaren ***
- 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
- ;*** KLAR ***
- ;*** Vanta en stund sa inte spelet gar for fort ***
- call DELAY_GAME_SPEED;
- ;*** KLAR ***
- ;*** Avgor om träff ***
- lds r16, POSX
- lds r17, TPOSX
- cp r16, r17
- brne NO_HIT
- lds r16, POSY
- lds r17, TPOSY
- cp r16, r17
- brne NO_HIT
- ldi r16, BEEP_LENGTH
- call BEEP
- call WARM
- NO_HIT:
- jmp RUN
- ; ---------------------------------------
- ; --- Multiplex display
- ; --- Uses : r16
- MUX:
- ;*** KLAR ***
- ;*** skriv rutin som handhar multiplexningen och ***
- ;*** utskriften till diodmatrisen. Oka SEED . ***
- push ZH
- push ZL
- push r16
- in r16, SREG
- push r16
- push r17
- ldi ZH,high(LINE)
- ldi ZL,low(LINE)
- ld r16, Z
- inc r16
- cpi r16, VMEM_SZ
- brne PRINT
- clr r16
- PRINT:
- st Z, r16
- ldi ZH,high(VMEM)
- ldi ZL,low(VMEM)
- add ZL, r16
- brcc NO_CARRY
- inc ZH
- NO_CARRY:
- clr r17
- out PORTB, r17
- ld r17, Z
- swap r16
- out PORTD, r16
- out portB, r17
- INCSRAM SEED
- pop r17
- pop r16
- out SREG, r16
- pop r16
- pop ZL
- pop ZH
- reti
- ; ---------------------------------------
- ; --- JOYSTICK Sense stick and update POSX , POSY
- ; --- Uses :
- JOYSTICK:
- ;*** skriv kod som okar eller minskar POSX beroende ***
- ;*** pa insignalen fran A/D - omvandlaren i X - led ... ***
- ;*** ... och samma for Y - led ***
- push r16
- clr r16
- ldi r16, (0<<ADLAR)|(1<<REFS1)|(1<<REFS0)|(0<<MUX0)|(0<<MUX1)
- out ADMUX, r16
- clr r16
- ldi r16, (1<<ADEN)|(1<<ADSC)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0)
- out ADCSRA, r16
- LOOP_x:
- sbic ADCSRA,ADSC
- jmp LOOP_X
- in r16, ADCH
- cpi r16, 3
- breq X_INC
- cpi r16, 0
- breq X_DEC
- rjmp JOYSTCK_Y
- X_DEC:
- INCSRAM POSX
- rjmp JOYSTCK_Y
- X_INC:
- DECSRAM POSX
- JOYSTCK_Y:
- clr r16
- ldi r16, (0<<ADLAR)|(1<<REFS1)|(1<<REFS0)|(1<<MUX0)|(0<<MUX1)
- out ADMUX, r16
- clr r16
- ldi r16, (1<<ADEN)|(1<<ADSC)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0)
- out ADCSRA,r16
- LOOP_Y:
- sbic ADCSRA,ADSC
- jmp LOOP_Y
- in r16, ADCH
- cpi r16, 3
- breq Y_INC
- cpi r16, 0
- breq Y_DEC
- rjmp KLAR
- Y_INC:
- INCSRAM POSY
- rjmp KLAR
- Y_DEC:
- DECSRAM POSY
- KLAR:
- pop r16
- JOY_LIM:
- call LIMITS ; don ’ t fall off world !
- 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 ; variable
- ldi r17 ,5 ; upper limit +1
- call POS_LIM ; actual work
- 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
- ; --- 1 st call Z points to POSX at entry and POSY at exit
- ; --- 2 nd 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 ; til done
- lsl r16 ; shift
- jmp SETBIT_LOOP
- SETBIT_END:
- ret
- ; ---------------------------------------
- ; --- Hardware init
- ; --- Uses :
- HW_INIT:
- ;*** KLAR ***
- ;*** Konfigurera hardvara och MUX - avbrott enligt ***
- ;*** ditt elektriska schema . Konfigurera ***
- ;*** flanktriggat avbrott pa INT0 ( PD2 ). ***
- ldi r16, $80
- out DDRA, r16
- ldi r16, $FF
- out DDRB, r16
- ldi r16, $70
- out DDRD, r16
- ldi r16,(1<<INT0)
- out GICR,r16
- ldi r16, (1<<ISC01)|(1<<ISC00)
- out MCUCR, r16
- sei ; display on
- ret
- ; ---------------------------------------
- ; --- WARM start . Set up a new game .
- ; --- Uses :
- WARM:
- ;*** KLAR ***
- ;*** Satt startposition ( POSX , POSY )=(0 ,2) ***
- push r16
- clr r16
- sts POSX, r16
- ldi r16, $2
- sts POSY, r16
- push r0
- push r0
- call RANDOM ; RANDOM returns TPOSX , TPOSY on stack
- ;*** KLAR ***
- ;*** Satt startposition ( TPOSX , TPOSY ) ***
- pop r16
- sts TPOSX, r16
- pop r16
- sts TPOSY, r16
- pop r16
- call ERASE
- ret
- ; ---------------------------------------
- ; --- RANDOM generate TPOSX , TPOSY
- ; --- in variables passed on stack .
- ; --- Usage as :
- ; --- push r0
- ; --- push r0
- ; --- call RANDOM
- ; --- pop TPOSX
- ; --- pop TPOSY
- ; --- Uses : r16
- RANDOM:
- ;*** KLAR ***
- ;*** Anvand SEED for att berakna TPOSX ***
- ;*** Anvand SEED for att berakna TPOSY ***
- ;*** ; store TPOSX 2..6
- ;*** ; store TPOSY 0..4
- in r16 , SPH
- mov ZH , r16
- in r16 , SPL
- mov ZL , r16
- lds r16 , SEED
- ;X
- mov r17, r16
- andi r17, $07
- cpi r17, $05
- brlo X_KLAR
- subi r17, 3
- X_KLAR:
- subi r17, -2
- std Z+3, r17
- ;Y
- mov r17, r16
- swap r17
- andi r17, $07
- cpi r17, $05
- brlo Y_KLAR
- subi r17, 3
- Y_KLAR:
- std Z+4, r17
- ret
- ; ---------------------------------------
- ; --- ERASE videomemory
- ; --- Clears VMEM .. VMEM +4
- ; --- Uses :
- ERASE:
- ;*** KLAR ***
- ;*** Radera videominnet ***
- push ZH
- push ZL
- push r16
- ldi ZH,high(VMEM)
- ldi ZL,low(VMEM)
- clr r16
- st z+, r16
- st z+, r16
- st z+, r16
- st z+, r16
- st z, r16
- pop r16
- pop ZL
- pop ZH
- ret
- ; ---------------------------------------
- ; --- BEEP ( r16 ) r16 half cycles of BEEP - PITCH
- ; --- Uses :
- BEEP:
- push r19
- push r18
- push r17
- ldi r19, BEEP_LENGTH
- INTE_KLAR:
- rcall LJUD
- dec r19
- brne INTE_KLAR
- pop r17
- pop r18
- pop r19
- ret
- LJUD:
- ldi r17, $10
- sbi PORTA, 7
- rcall TON_BEEP
- ldi r17, $10
- cbi PORTA,7
- rcall TON_BEEP
- dec r19
- BRNE BEEP
- ret
- TON_BEEP:
- ldi r18, BEEP_PITCH
- WAIT:
- dec r18
- BRNE WAIT
- dec r17
- BRNE TON_BEEP
- ret
- ; ---------EGET------------------
- DELAY_GAME_SPEED:
- push r16
- push r17
- push r18
- ldi r18, GAME_SPEED
- WAIT_MORE:
- rcall WAIT_MS
- dec r18
- brne WAIT_MORE
- pop r18
- pop r17
- pop r16
- ret
- WAIT_MS:
- ldi r16,$10 ; Decimal bas
- delayYttreLoop:
- ldi r17,$1F
- delayInreLoop:
- dec r17
- brne delayInreLoop
- dec r16
- brne delayYttreLoop
- ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement