Advertisement
Sumss

lab4v0.7

Dec 16th, 2018
433
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     ; --- lab4_skal . asm
  2.     .equ VMEM_SZ = 5 ; # rows on display
  3.     .equ AD_CHAN_X = 0 ; ADC0 = PA0 , PORTA bit 0 X - led
  4.     .equ AD_CHAN_Y = 1 ; ADC1 = PA1 , PORTA bit 1 Y - led
  5.     .equ GAME_SPEED = 70 ; inter - run delay ( millisecs )
  6.     .equ PRESCALE = 7 ; AD - prescaler value
  7.     .equ BEEP_PITCH = 20 ; Victory beep pitch
  8.     .equ BEEP_LENGTH = 100 ; Victory beep length
  9.  
  10.     ; ---------------------------------------
  11.     ; --- Memory layout in SRAM
  12.     .dseg
  13.     .org SRAM_START
  14. POSX:   .byte 1 ; Own position
  15. POSY:   .byte 1
  16. TPOSX:  .byte 1 ; Target position
  17. TPOSY:  .byte 1
  18. LINE:   .byte 1 ; Current line
  19. VMEM:   .byte VMEM_SZ ; Video MEMory
  20. SEED:   .byte 1 ; Seed for Random
  21.  
  22.     ; ---------------------------------------
  23.     ; --- Macros for inc / dec - rementing
  24.     ; --- a byte in SRAM
  25.     .macro INCSRAM ; inc byte in SRAM
  26.     lds r16 , @0
  27.     inc r16
  28.     sts @0 , r16
  29.     .endmacro
  30.     .macro DECSRAM ; dec byte in SRAM
  31.     lds r16 , @0
  32.     dec r16
  33.     sts @0 , r16
  34.     .endmacro
  35.     ; ---------------------------------------
  36.     ; --- Code
  37.     .cseg
  38.     .org $0
  39.     jmp START
  40.     .org INT0addr
  41.     jmp MUX
  42.  
  43. START:
  44.     ;*** KLAR ***
  45.     ;*** Sätt stackpekaren ***
  46.     ldi r16, high(RAMEND)
  47.     out sph, r16
  48.     ldi r16, low(ramend)
  49.     out spl, r16
  50.  
  51.     call HW_INIT
  52.     call WARM
  53. RUN:
  54.     call JOYSTICK
  55.     call ERASE
  56.     call UPDATE
  57.  
  58.     ;*** KLAR ***
  59.     ;*** Vanta en stund sa inte spelet gar for fort ***
  60.     call DELAY_GAME_SPEED;
  61.  
  62.     ;*** KLAR ***
  63.     ;*** Avgor om träff ***
  64.     lds r16, POSX
  65.     lds r17, TPOSX
  66.     cp r16, r17
  67.     brne NO_HIT
  68.     lds r16, POSY
  69.     lds r17, TPOSY
  70.     cp r16, r17
  71.     brne NO_HIT
  72.  
  73.     ldi r16, BEEP_LENGTH
  74.     call BEEP
  75.     call WARM
  76. NO_HIT:
  77.     jmp RUN
  78.     ; ---------------------------------------
  79.     ; --- Multiplex display
  80.     ; --- Uses : r16
  81. MUX:
  82.     ;*** KLAR ***
  83.     ;*** skriv rutin som handhar multiplexningen och ***
  84.     ;*** utskriften till diodmatrisen. Oka SEED . ***
  85.     push ZH
  86.     push ZL
  87.     push r16
  88.     in r16, SREG
  89.     push r16
  90.     push r17
  91.  
  92.     ldi ZH,high(LINE)
  93.     ldi ZL,low(LINE)
  94.     ld r16, Z
  95.     inc r16
  96.     cpi r16, VMEM_SZ
  97.     brne PRINT
  98.     clr r16
  99. PRINT:
  100.     st Z, r16
  101.     ldi ZH,high(VMEM)
  102.     ldi ZL,low(VMEM)
  103.     add ZL, r16
  104.     brcc NO_CARRY
  105.     inc ZH
  106. NO_CARRY:
  107.     clr r17
  108.     out PORTB, r17
  109.     ld r17, Z
  110.     swap r16
  111.     out PORTD, r16
  112.     out portB, r17
  113.  
  114.     INCSRAM SEED
  115.  
  116.     pop r17
  117.     pop r16
  118.     out SREG, r16
  119.     pop r16
  120.     pop ZL
  121.     pop ZH
  122.     reti
  123.    
  124.     ; ---------------------------------------
  125.     ; --- JOYSTICK Sense stick and update POSX , POSY
  126.     ; --- Uses :
  127. JOYSTICK:
  128.     ;*** skriv kod som okar eller minskar POSX beroende ***
  129.     ;*** pa insignalen fran A/D - omvandlaren i X - led ... ***
  130.     ;*** ... och samma for Y - led ***
  131.  
  132.     push r16
  133.     clr r16
  134.     ldi r16, (0<<ADLAR)|(1<<REFS1)|(1<<REFS0)|(0<<MUX0)|(0<<MUX1)
  135.     out ADMUX, r16
  136.  
  137.     clr r16
  138.     ldi r16, (1<<ADEN)|(1<<ADSC)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0)
  139.     out ADCSRA, r16
  140.  
  141. LOOP_x:
  142.     sbic ADCSRA,ADSC
  143.     jmp LOOP_X
  144.  
  145.     in r16, ADCH
  146.     cpi r16, 3
  147.     breq X_INC
  148.     cpi r16, 0
  149.     breq X_DEC
  150.     rjmp JOYSTCK_Y
  151.  
  152. X_DEC:
  153.     INCSRAM POSX
  154.     rjmp JOYSTCK_Y
  155.  
  156. X_INC:
  157.     DECSRAM POSX
  158.  
  159. JOYSTCK_Y:
  160.     clr r16
  161.     ldi r16, (0<<ADLAR)|(1<<REFS1)|(1<<REFS0)|(1<<MUX0)|(0<<MUX1)
  162.     out ADMUX, r16
  163.  
  164.     clr r16
  165.     ldi r16, (1<<ADEN)|(1<<ADSC)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0)
  166.     out ADCSRA,r16
  167.  
  168. LOOP_Y:
  169.     sbic ADCSRA,ADSC
  170.     jmp LOOP_Y
  171.  
  172.     in r16, ADCH
  173.     cpi r16, 3
  174.     breq Y_INC
  175.     cpi r16, 0
  176.     breq Y_DEC
  177.     rjmp KLAR
  178.  
  179. Y_INC:
  180.     INCSRAM POSY
  181.     rjmp KLAR
  182.  
  183. Y_DEC:
  184.     DECSRAM POSY
  185.  
  186. KLAR:
  187.     pop r16
  188.  
  189. JOY_LIM:
  190.     call LIMITS ; don ’ t fall off world !
  191.     ret
  192. ; ---------------------------------------
  193. ; --- LIMITS Limit POSX , POSY coordinates
  194. ; --- Uses : r16 , r17
  195. LIMITS:
  196.     lds r16 , POSX ; variable
  197.     ldi r17 ,7 ; upper limit +1
  198.     call POS_LIM ; actual work
  199.     sts POSX , r16
  200.     lds r16 , POSY ; variable
  201.     ldi r17 ,5 ; upper limit +1
  202.     call POS_LIM ; actual work
  203.     sts POSY , r16
  204.     ret
  205. POS_LIM:
  206.     ori r16 ,0 ; negative ?
  207.     brmi POS_LESS ; POSX neg = > add 1
  208.     cp r16 , r17 ; past edge
  209.     brne POS_OK
  210.     subi r16 ,2
  211. POS_LESS:
  212.     inc r16
  213. POS_OK:
  214.     ret
  215.  
  216.     ; ---------------------------------------
  217.     ; --- UPDATE VMEM
  218.     ; --- with POSX /Y , TPOSX /Y
  219.     ; --- Uses : r16 , r17 , Z
  220. UPDATE:
  221.     clr ZH
  222.     ldi ZL, LOW(POSX)
  223.     call SETPOS
  224.     clr ZH
  225.     ldi ZL, LOW(TPOSX)
  226.     call SETPOS
  227.     ret
  228.     ; --- SETPOS Set bit pattern of r16 into * Z
  229.     ; --- Uses : r16 , r17 , Z
  230.     ; --- 1 st call Z points to POSX at entry and POSY at exit
  231.     ; --- 2 nd call Z points to TPOSX at entry and TPOSY at exit
  232. SETPOS:
  233.     ld r17, Z + ; r17 = POSX
  234.     call SETBIT ; r16 = bitpattern for VMEM + POSY
  235.     ld r17 , Z ; r17 = POSY Z to POSY
  236.     ldi ZL , LOW ( VMEM )
  237.     add ZL , r17 ; Z= VMEM + POSY , ZL = VMEM +0..4
  238.     ld r17 , Z ; current line in VMEM
  239.     or r17 , r16 ; OR on place
  240.     st Z , r17 ; put back into VMEM
  241.     ret
  242.     ; --- SETBIT Set bit r17 on r16
  243.     ; --- Uses : r16 , r17
  244. SETBIT:
  245.     ldi r16 , $01 ; bit to shift
  246. SETBIT_LOOP:
  247.     dec r17
  248.     brmi SETBIT_END ; til done
  249.     lsl r16 ; shift
  250.     jmp SETBIT_LOOP
  251. SETBIT_END:
  252.     ret
  253.     ; ---------------------------------------
  254.     ; --- Hardware init
  255.     ; --- Uses :
  256. HW_INIT:
  257.     ;*** KLAR ***
  258.     ;*** Konfigurera hardvara och MUX - avbrott enligt ***
  259.     ;*** ditt elektriska schema . Konfigurera ***
  260.     ;*** flanktriggat avbrott pa INT0 ( PD2 ). ***
  261.     ldi r16, $80
  262.     out DDRA, r16
  263.     ldi r16, $FF
  264.     out DDRB, r16
  265.     ldi r16, $70
  266.     out DDRD, r16
  267.     ldi r16,(1<<INT0)
  268.     out GICR,r16
  269.     ldi r16, (1<<ISC01)|(1<<ISC00)
  270.     out MCUCR, r16
  271.     sei ; display on
  272.     ret
  273.  
  274.     ; ---------------------------------------
  275.     ; --- WARM start . Set up a new game .
  276.     ; --- Uses :
  277. WARM:
  278.     ;*** KLAR ***
  279.     ;*** Satt startposition ( POSX , POSY )=(0 ,2) ***
  280.     push r16
  281.     clr r16
  282.     sts POSX, r16
  283.     ldi r16, $2
  284.     sts POSY, r16
  285.     push r0
  286.     push r0
  287.     call RANDOM ; RANDOM returns TPOSX , TPOSY on stack
  288.     ;*** KLAR ***
  289.     ;*** Satt startposition ( TPOSX , TPOSY ) ***
  290.     pop r16
  291.     sts TPOSX, r16
  292.     pop r16
  293.     sts TPOSY, r16
  294.  
  295.     pop r16
  296.     call ERASE
  297.     ret
  298.     ; ---------------------------------------
  299.     ; --- RANDOM generate TPOSX , TPOSY
  300.     ; --- in variables passed on stack .
  301.     ; --- Usage as :
  302.     ; --- push r0
  303.     ; --- push r0
  304.     ; --- call RANDOM
  305.     ; --- pop TPOSX
  306.     ; --- pop TPOSY
  307.     ; --- Uses : r16
  308. RANDOM:
  309.     ;*** KLAR ***
  310.     ;*** Anvand SEED for att berakna TPOSX ***
  311.     ;*** Anvand SEED for att berakna TPOSY ***
  312.     ;*** ; store TPOSX 2..6
  313.     ;*** ; store TPOSY 0..4
  314.     in r16 , SPH
  315.     mov ZH , r16
  316.     in r16 , SPL
  317.     mov ZL , r16
  318.     lds r16 , SEED
  319.    
  320.    
  321.     ;X
  322.     mov r17, r16
  323.     andi r17, $07
  324.     cpi r17, $05
  325.     brlo X_KLAR
  326.     subi r17, 3
  327. X_KLAR:
  328.     subi r17, -2
  329.     std Z+3, r17
  330.     ;Y
  331.     mov r17, r16
  332.     swap r17
  333.     andi r17, $07
  334.     cpi r17, $05
  335.     brlo Y_KLAR
  336.     subi r17, 3
  337. Y_KLAR:
  338.     std Z+4, r17
  339.     ret
  340.     ; ---------------------------------------
  341.     ; --- ERASE videomemory
  342.     ; --- Clears VMEM .. VMEM +4
  343.     ; --- Uses :
  344. ERASE:
  345.     ;*** KLAR ***
  346.     ;*** Radera videominnet ***
  347.     push ZH
  348.     push ZL
  349.     push r16
  350.  
  351.     ldi ZH,high(VMEM)
  352.     ldi ZL,low(VMEM)
  353.     clr r16
  354.     st z+, r16
  355.     st z+, r16
  356.     st z+, r16
  357.     st z+, r16
  358.     st z, r16
  359.  
  360.     pop r16
  361.     pop ZL
  362.     pop ZH
  363.  
  364.     ret
  365.     ; ---------------------------------------
  366.     ; --- BEEP ( r16 ) r16 half cycles of BEEP - PITCH
  367.     ; --- Uses :
  368. BEEP:
  369.     push r19
  370.     push r18
  371.     push r17
  372.     ldi r19, BEEP_LENGTH
  373. INTE_KLAR:
  374.     rcall LJUD
  375.     dec r19
  376.     brne INTE_KLAR
  377.     pop r17
  378.     pop r18
  379.     pop r19
  380.     ret
  381. LJUD:
  382.     ldi r17, $10
  383.     sbi PORTA, 7
  384.     rcall TON_BEEP
  385.     ldi r17, $10
  386.     cbi PORTA,7
  387.     rcall TON_BEEP
  388.     dec r19
  389.     BRNE BEEP
  390.     ret
  391.  
  392. TON_BEEP:
  393.     ldi r18, BEEP_PITCH
  394. WAIT:
  395.     dec r18
  396.     BRNE WAIT
  397.     dec r17
  398.     BRNE TON_BEEP
  399.     ret
  400.  
  401.  
  402.  
  403. ; ---------EGET------------------
  404. DELAY_GAME_SPEED:
  405.     push r16
  406.     push r17
  407.     push r18
  408.     ldi r18, GAME_SPEED
  409. WAIT_MORE:
  410.     rcall WAIT_MS
  411.     dec r18
  412.     brne WAIT_MORE
  413.     pop r18
  414.     pop r17
  415.     pop r16
  416.     ret
  417.  
  418. WAIT_MS:
  419.     ldi r16,$10 ; Decimal bas
  420. delayYttreLoop:
  421.     ldi r17,$1F
  422. delayInreLoop:
  423.     dec r17
  424.     brne delayInreLoop
  425.     dec r16
  426.     brne delayYttreLoop
  427.     ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement