OpenCores
URL https://opencores.org/ocsvn/RISCMCU/RISCMCU/trunk

Subversion Repositories RISCMCU

[/] [RISCMCU/] [trunk/] [asm/] [memory_game.asm] - Blame information for rev 9

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 9 yapzihe
.include "riscmcu.inc"
2
 
3
.def    temp = r16
4
.def    times = r17
5
.def    num = r18
6
.def    stack = r19
7
.def    counter = r20
8
.def    life = r22
9
.def    level = r23
10
.def    temp2 = r24
11
 
12
.cseg
13
        rjmp    reset
14
        reti
15
        reti
16
 
17
reset:
18
        ldi     counter,5
19
 
20
        ser     temp
21
        out     ddrb,temp       ; Port B direction as OUTPUT
22
        out     ddrd,temp       ; Port D direction as OUTPUT
23
        out     portc,temp
24
        out     portd,temp
25
 
26
        ldi     temp,1
27
        out     tccr0,temp      ; set clock as timer clock source
28
        ldi     temp,$0F
29
        out     ddrc,temp       ; PinC[3..0] output, [7..4] input
30
 
31
start:
32
        ldi     life,3
33
        ldi     level,1
34
        clt                     ; T = 0, start with correct
35
 
36
        rcall   getinput        ; hit any key to start the game
37
        rcall   showlife
38
        out     portB,level     ; output level to display
39
        rcall   holding         ; release?
40
 
41
nextlevel:
42
        rcall   delay           ; wait a while before the game is started
43
        rcall   showled         ; show the LEDs and save it in buffer
44
nlife:  rcall   check           ; get input and check
45
        brts    wrong
46
        rcall   greenled
47
        inc     level           ; when win, level <- level + 1
48
        out     portB,level     ; output level to display
49
        rjmp    nextlevel
50
wrong:  rcall   redled
51
        rcall   delay
52
        dec     life
53
        rcall   showlife
54
        out     portb,level
55
        rcall   delay
56
        cpi     life,0
57
        breq    start
58
        rcall   playback
59
        clt
60
        rjmp    nlife
61
 
62
 
63
;*******************************************
64
; show life
65
showlife:
66
        mov     temp,life
67
        sbr     temp,$f0
68
        out     portb,temp
69
        rcall   delay
70
        rcall   delay
71
        ret
72
 
73
;*******************************************
74
; on red leds
75
 
76
redled:
77
        clr     temp
78
        out     portc,temp
79
        rcall   delay
80
        ser     temp
81
        out     portc,temp
82
        ret
83
 
84
;*******************************************
85
; on green led
86
 
87
greenled:
88
        cbi     portd,7
89
        rcall   delay
90
        sbi     portd,7
91
        ret
92
 
93
 
94
;*******************************************
95
; play back the last sequence when wrong
96
 
97
playback:
98
        rcall   init
99
w10:    ld      temp,Z+
100
        out     portC,temp
101
        rcall   delay
102
        ser     temp            ; OFF
103
        out     portC,temp
104
        ldi     counter,1
105
        rcall   delay
106
        dec     times
107
        brne    w10
108
        ret
109
 
110
;*******************************************
111
; Show sequence of LEDs (base on times), save in buffer
112
 
113
showled:
114
        rcall   init
115
snext:  rcall   random          ; return in NUM
116
        st      Z+,num
117
        out     portC,num       ; display the LED
118
        rcall   delay
119
        ser     temp            ; OFF
120
        out     portC,temp
121
        ldi     counter,1
122
        rcall   delay
123
        dec     times
124
        brne    snext
125
        ret
126
 
127
 
128
;*******************************************
129
; wait for input and check
130
; return T = 0 when corrert, T = 1 when wrong
131
 
132
check:
133
        rcall   init
134
nextkey:
135
        rcall   holding
136
        sei
137
        rcall   getinput        ; return in NUM
138
        cli
139
        ld      temp,Z+
140
        cpse    num,temp
141
        set                     ; Set T flag if one key is wrong
142
        rcall   holding
143
        rcall   sdelay
144
        dec     times
145
        brne    nextkey
146
        ret
147
 
148
;*******************************************
149
; still holding the key ?
150
 
151
holding:
152
        in      temp,pinc
153
        cbr     temp,$0F
154
        cpi     temp,$F0
155
        brne    holding
156
        ret
157
 
158
;*******************************************
159
; getinput
160
 
161
getinput:
162
g15:    in      num,pinc
163
        cbr     num,$0F ; clear lower nibble
164
        cpi     num,$F0
165
        breq    g15
166
        rcall   sdelay
167
        in      temp,pinc
168
        cbr     temp,$0F
169
        cp      temp,num
170
        brne    g15
171
        swap    num
172
        ret
173
 
174
;*******************************************
175
; 1. Load level to times
176
; 2. ZP point to start of buffer
177
 
178
init:   mov     times,level     ; times <- level
179
        ldi     ZP,buffer       ; ZP <- buffer
180
        ret
181
 
182
;*******************************************
183
; Generate Random Num, return in NUM
184
 
185
random:
186
        in      num,tcnt0
187
ran:    cpi     num,4
188
        brlo    rnext
189
        subi    num,4
190
        rjmp    ran
191
rnext:  cpi     num,0
192
        brne    r10
193
        ldi     num,0b1110
194
        rjmp    rend
195
r10:    cpi     num,1
196
        brne    r20
197
        ldi     num,0b1101
198
        rjmp    rend
199
r20:    cpi     num,2
200
        brne    r30
201
        ldi     num,0b1011
202
        rjmp    rend
203
r30:    ldi     num,0b0111
204
rend:   in      temp,tcnt0
205
        ror     temp
206
        ror     temp
207
        out     tcnt0,temp
208
        ret
209
 
210
;*******************************************
211
delay:
212
        mov     stack,ZP
213
del:    ldi     ZP,count
214
        ld      temp,Z
215
        dec     temp
216
        st      Z,temp
217
        brne    del
218
        ldi     ZP,count+1
219
        ld      temp,Z
220
        dec     temp
221
        st      Z,temp
222
        brne    del
223
        dec     counter
224
        brne    del
225
        ldi     counter,5
226
        mov     ZP,stack
227
        ret
228
 
229
sdelay:
230
        clr     temp
231
sd5:    dec     temp
232
        brne    sd5
233
        ret
234
;*******************************************
235
.dseg
236
count:  .byte   2
237
buffer: .byte   20

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.