previous vasm version treats the \ in row7 as the start of an escape sequence
[zxkeybtest] / zxkeybtest.asm
1 ; vi:filetype=z80:
2 FB_ADDR equ $4000
3 ATTR_ADDR equ $5800
4 ULA_PORT equ $fe
5 START_ROW equ 3
6
7         macro PRINTSTR, row, col, str
8         push bc
9         push de
10         ld b, \row
11         ld c, \col
12         call calc_cell_addr
13         ld hl, \str
14         call putstr
15         pop de
16         pop bc
17         endm
18
19         macro PRINTVAL, row, col, str
20         push bc
21         push de
22         push hl
23         push af
24         ld b, \row
25         ld c, \col
26         call calc_cell_addr
27         ld hl, \str
28         call putstr
29         ld hl, \str
30         call strlen
31         add e
32         ld e, a
33         pop af
34         call putbin8
35         pop hl
36         pop de
37         pop bc
38         endm
39
40         
41
42 main:
43         call init
44
45         ld hl, FB_ADDR + 25
46         call draw_logo
47
48         PRINTSTR START_ROW, 0, str_row0
49         PRINTSTR START_ROW + 1, 0, str_row1
50         PRINTSTR START_ROW + 2, 0, str_row2
51         PRINTSTR START_ROW + 3, 0, str_row3
52         PRINTSTR START_ROW + 4, 0, str_row4
53         PRINTSTR START_ROW + 5, 0, str_row5
54         PRINTSTR START_ROW + 6, 0, str_row6
55         PRINTSTR START_ROW + 7, 0, str_row7
56
57 .mainloop:
58         ld bc, $fefe    ; c: port $fe, b: row select $fe
59         xor a
60 .rowloop:
61         push af
62         push bc
63         add START_ROW
64         ld b, a
65         ld c, 17
66         call calc_cell_addr
67         
68         pop bc
69         in a, (c)
70         call putbin5
71         rlc b
72         pop af
73         inc a
74         cp $8
75         jr nz, .rowloop
76         jr .mainloop
77
78 init:
79         ; setup attributes
80         ld hl, ATTR_ADDR
81         ld a, $47
82         ld b, 24
83 .attry: ld c, 32
84 .attrx: ld (hl), a
85         inc hl
86         dec c
87         jr nz, .attrx
88         dec b
89         jr nz, .attry
90
91         ; clear screen
92         ld hl, FB_ADDR
93         xor a
94         ld b, 192
95 .clry:  ld c, 32
96 .clrx:  ld (hl), a
97         inc hl
98         dec c
99         jr nz, .clrx
100         dec b
101         jr nz, .clry
102
103         ; set border
104         out (ULA_PORT), a
105         
106         ret
107
108 ; expects X -> c, Y -> b, returns in de
109 calc_cell_addr:
110         push af
111         sla b
112         sla b           ; change from blocks to pixels
113         sla b
114         ; construct low address byte -> e
115         ld a, b         ; start with Y
116         sla a
117         sla a
118         and $e0         ; keep top 3 bits
119         ld e, a         ; move into e
120         ld a, c         ; a <- X
121         and $1f         ; keep low 5 bits
122         or e            ; combine with Y bits
123         ld e, a         ; move the result back to e
124         ; construct high address byte -> d
125         ld a, b         ; start with Y again
126         sra a
127         sra a
128         sra a
129         and $18         ; keep bits 3 and 4
130         ld d, a         ; keep it in d
131         ld a, b         ; grap Y one more time
132         and $7          ; keep low 3 bits of Y in a
133         or d            ; combine with Y6-Y7
134         ld bc, FB_ADDR
135         or b            ; combine with high byte of fb address
136         ld d, a         ; move result back to d
137         pop af
138         ret
139
140         ; hl: dest
141 draw_logo:
142         push hl
143         ld de, logo
144         ld b, 16
145 .logoy: ld c, 7
146         push bc
147 .logox: ld a, (de)
148         ld (hl), a
149         inc de
150         inc l
151         dec c
152         jr nz, .logox
153         ld bc, 7
154         sbc hl, bc
155         inc h
156
157         ld a, h
158         and $e7
159         ld h, a 
160         and $7
161         jr nz, .skipinc
162         ld a, l
163         add $20
164         ld l, a
165 .skipinc:
166         pop bc
167         dec b
168         jr nz, .logoy
169         pop hl
170
171         ; attr
172         ld a, h
173         srl a
174         srl a
175         srl a
176         or $58
177         ld h, a
178         ld a, $42
179
180         push hl
181         pop ix
182         ld b, 7
183 .sattr: ld (ix), a
184         ld (ix + 32), a
185         inc ix
186         dec b
187         jr nz, .sattr
188         ret
189
190         ; de: dest
191         ; a: char
192 putchar:
193         ; is it a digit?
194         sub '0'
195         ret c
196         cp 11
197         jr nc, .notdigit
198         push bc
199         ld bc, glyph_0
200         jr .common
201 .notdigit:
202         ; is it A-Z ?
203         sub 'A'-'0'
204         ret c
205         cp 30
206         ret nc
207         push bc
208         ld bc, glyph_a
209 .common:
210         push de
211         push hl
212         sla a
213         sla a
214         sla a
215         ld h, 0
216         ld l, a
217         add hl, bc
218         ld c, 8
219 .chartop:
220         ld a, (hl)
221         ld (de), a
222         inc d
223         inc hl
224         dec c
225         jr nz, .chartop
226         pop hl
227         pop de
228         pop bc
229         ret
230
231         ; de: dest
232         ; hl: str
233 putstr:
234         push de
235         push bc
236 .loop:  xor a
237         ld b, (hl)
238         cp b
239         jr z, .done
240         ld a, b
241         call putchar
242         inc hl
243         inc e
244         jr .loop
245 .done:  pop bc
246         pop de
247         ret
248
249         ; de: dest
250         ; a: number
251 putbin8:
252         push de
253         push bc
254         ld b, 8
255 .loop:  rlca
256         push af
257         and 1
258         add '0'
259         call putchar
260         pop af
261         inc e
262         dec b
263         jr nz, .loop
264         pop bc
265         pop de
266         ret
267
268 putbin5:
269         push de
270         push bc
271         ld b, 5
272         rlca
273         rlca
274         rlca
275         jr putbin8.loop
276
277         ; hl: string
278         ; return -> a
279 strlen:
280         push hl
281         xor a
282         ld b, a
283 .loop:  cp (hl)
284         jr z, .done
285         inc hl
286         inc b
287         jr .loop
288 .done:  ld a, b
289         pop hl
290         ret
291
292         
293 str_row0 asciiz 'ROW0: V C X Z [: '
294 str_row1 asciiz 'ROW1: G F D S A: '
295 str_row2 asciiz 'ROW2: T R E W Q: '
296 str_row3 asciiz 'ROW3: 5 4 3 2 1: '
297 str_row4 asciiz 'ROW4: 6 7 8 9 0: '
298 str_row5 asciiz 'ROW5: Y U I O P: '
299 str_row6 asciiz 'ROW6: H J K L ^: '
300 str_row7 asciiz 'ROW7: B N M ] \\: '
301
302         include glyphs.inc
303         include logo.inc