-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrom.asm
477 lines (376 loc) · 11.7 KB
/
rom.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
; myZ80, A computer based on Z80 microprocessor
; Copyright (C) 2020-2021 Cidorvan Leite
; This program is free software: you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation, either version 3 of the License, or
; (at your option) any later version.
; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
; GNU General Public License for more details.
; You should have received a copy of the GNU General Public License
; along with this program. If not, see [http://www.gnu.org/licenses/].
INCLUDE "rom.inc"
ORG 0000h
JP main ; 0000h
ORG 0010h
JP PSGinit ; 0010h
ORG 0020h
JP DSPinit ; 0020h
JP DSPwait ; 0023h
JP DSPwriteRegister ; 0026h
JP DSPscrollUp ; 0029h
JP DSPprintChar ; 002Ch
JP DSPprintString ; 002Fh
JP DSPprintHex ; 0032h
JP DSPprintDec ; 0035h
JP DSPprintDec16 ; 0038h
JP DSPprintBin ; 003Bh
ORG 0050h
JP waitForKeyPressed ; 0050h
ORG 0100h
main:
DI
LD SP, 0 ; Init stack pointer
CALL PSGinit ; Initialize sound/IO
CALL DSPinit ; Initialize display
LD HL, ready
CALL DSPprintString ; Display ready message
CALL loadBinary ; Load binary from UART
IFDEF DUMP_FIRST_BYTES ; Show first bytes for DEBUG
LD A, CLEAR_DISPLAY
CALL DSPwriteRegister ; Clear display
LD HL, RAM_START_POINTER
CALL .DUMP ; Print first two bytes in binary
LD A, SET_DDRAM_ADDR | 20
CALL DSPwriteRegister
INC HL
CALL .DUMP ; Print second two bytes in binary
LD A, SET_DDRAM_ADDR | 64
CALL DSPwriteRegister ; Move cursor to second line
LD A, 0
LD (RAM_START_POINTER + 028h), A
LD HL, RAM_START_POINTER
CALL DSPprintString ; Print first 40 bytes received
HALT ; Wait for reset
.DUMP: LD A, (HL)
CALL DSPprintBin ; Print current byte
LD A, ' '
CALL DSPprintChar ; Print space
INC HL
LD A, (HL)
CALL DSPprintBin ; Print next byte
RET
ELSE
LD DE, RAM_START_POINTER
SBC HL, DE
LD A, SET_DDRAM_ADDR | 64
CALL DSPwriteRegister
CALL DSPprintDec16
LD HL, loaded
CALL DSPprintString ; Display how many bytes were read
LD A, SET_DDRAM_ADDR | 20
CALL DSPwriteRegister
LD HL, press
CALL DSPprintString ; Display press any key message
CALL waitForKeyPressed ; Wait any key to be pressed to continue
LD A, CLEAR_DISPLAY
CALL DSPwriteRegister ; Clear display
JP RAM_START_POINTER ; Everything done, let's play!!!!
ENDIF
; ** !!!! This function can not be optimized !!!!
; ** Load binary from UART at 115200baud with 6MHz clock
; ********************************************************
loadBinary:
LD A, 14
OUT (PSG_W_ADDRESS), A ; Define PSG PORTA to read
LD HL, RAM_START_POINTER ; Address to store all binary
.L0: LD B, 0
.L1: IN A, (PSG_R_DATA) ; 11
BIT 7, A ; 8
JP Z, .L2 ; 10
DJNZ .L1 ; 13/8
JP .L4 ; 10 ; Synchronizing with start bit
.L2: NOP
NOP
LD C, 8 ; 7
LD D, 0 ; 7 ; Synchronized, starting to read each bit
.L3: SRL D ; 8
IN A, (PSG_R_DATA) ; 11
AND 080h ; 7
OR D ; 4
LD D, A ; 4 ; Bit stored
NOP
DEC C ; 4
JP NZ, .L3 ; 10 ; Delay to read next bit
LD (HL), D ; 7
INC HL ; 6
JP .L0 ; 10 ; Byte stored, let's read next one
.L4: LD A, H
OR L
CP RAM_START_POINTER >> 8
JP Z, .L0 ; HL not changed, try again...
RET
; *******************
; ** PSG functions **
; *******************
; PSG_INIT (0010h)
; Entry ..... None
; Exit ...... None
; Modifies .. AF
; Inititialize PSG with PA input, PB output, noise and tone disable
; *******************************************************************
PSGinit:
LD A, 7 ; Mixer - I/O
OUT (PSG_W_ADDRESS), A
LD A, 0BFh ; PA input, PB output, noise and tone disable
OUT (PSG_W_DATA), A
LD A, 15 ; PORTB
OUT (PSG_W_ADDRESS), A
LD A, 0FFh ; Every pin HIGH
OUT (PSG_W_DATA), A
RET
; ***********************
; ** Display functions **
; ***********************
; DSP_INIT (0020h)
; Entry ..... None
; Exit ...... None
; Modifies .. AF
; Initialize display with 8bits and two lines
; *********************************************
DSPinit:
LD A, FUNCTION_SET | DL_8BITS | N_2LINES
CALL DSPwriteRegister
LD A, DISPLAY_CONTROL | D_DISPLAY_ON
CALL DSPwriteRegister
LD A, CLEAR_DISPLAY
CALL DSPwriteRegister
RET
; DSP_WAIT (0023h)
; Entry ..... None
; Exit ...... None
; Modifies .. None
; Wait last command to finish
; *****************************
DSPwait:
PUSH AF
.L0: IN A, (DISPLAY_REG)
BIT 7, A
JR Z, .L1
NOP
NOP
NOP
NOP
JR .L0
.L1: POP AF
RET
; DSP_WRITE_REG (0026h)
; Entry ..... A=Value
; Exit ...... None
; Modifies .. AF
; Write value to display internal register
; ******************************************
DSPwriteRegister:
CALL DSPwait
OUT (DISPLAY_REG), A
RET
; DSP_SCROLL_UP (0029h)
; Entry ..... None
; Exit ...... None
; Modifies .. AF B HL
; Scroll display one line up
; ****************************
DSPscrollUp:
LD HL, -20
ADD HL, SP
LD SP, HL ; allocate 20 bytes in stack
LD A, SET_DDRAM_ADDR | 64
CALL .CL
LD A, SET_DDRAM_ADDR
CALL .WL ; move second line to first one
LD A, SET_DDRAM_ADDR | 20
CALL .CL
LD A, SET_DDRAM_ADDR | 64
CALL .WL ; move third line to second one
LD A, SET_DDRAM_ADDR | 84
CALL .CL
LD A, SET_DDRAM_ADDR | 20
CALL .WL ; move forth line to third one
LD A, SET_DDRAM_ADDR | 84
CALL DSPwriteRegister
LD A, ' '
LD B, 20
.S0: CALL DSPprintChar
DJNZ .S0 ; clean last line with spaces
LD SP, HL
RET
.CL: CALL DSPwriteRegister
LD B, 20
LD HL, 2
ADD HL, SP
.CL0: CALL DSPwait
IN A, (DISPLAY_DATA)
LD (HL), A ; copy full line to stack
INC HL
DJNZ .CL0
RET
.WL: CALL DSPwriteRegister
LD B, 20
LD HL, 2
ADD HL, SP
.WL0: LD A, (HL) ; copy full line from stack
INC HL
CALL DSPprintChar
DJNZ .WL0
RET
; DSP_PRINT_CHAR (002Ch)
; Entry ..... A=ASCII code
; Exit ...... None
; Modifies .. AF
; Print one character in current cursor position
; ************************************************
DSPprintChar:
CALL DSPwait
OUT (DISPLAY_DATA), A
RET
; DSP_PRINT_STRING (002Fh)
; Entry ..... HL=String pointer
; Exit ...... None
; Modifies .. AF HL
; Print string in current cursor position
; *****************************************
DSPprintString:
LD A, (HL)
OR A
RET Z
CALL DSPwait
OUT (DISPLAY_DATA), A
INC HL
JR DSPprintString
; DSP_PRINT_HEX (0032h)
; Entry ..... A=Value
; Exit ...... None
; Modifies .. AF
; Print an 8bits hexadecimal number in current cursor position
; **************************************************************
DSPprintHex:
PUSH AF
SRL A
SRL A
SRL A
SRL A
CALL .PH0
POP AF
.PH0: AND 0Fh
CP 10
JR C, .PH1
ADD 'A' - '0' - 10
.PH1: ADD '0'
CALL DSPprintChar
RET
; DSP_PRINT_DEC (0035h)
; Entry ..... A=Value
; Exit ...... None
; Modifies .. AF BC
; Print an 8bit decimal in current cursor position
; **************************************************
DSPprintDec:
LD BC, 100
CALL PD1
PD0: LD BC, 10
CALL PD1
ADD '0'
CALL DSPprintChar
RET
PD1: CP C
JR C, .PD2
INC B
SUB C
JR PD1
.PD2: PUSH AF
LD A, B
ADD '0'
CALL DSPprintChar
POP AF
RET
; DSP_PRINT_DEC16 (0038h)
; Entry ..... HL=Value
; Exit ...... None
; Modifies .. AF BC DE HL
; Print a 16bit decimal in current cursor position
; **************************************************
DSPprintDec16:
LD DE, 10000
CALL .PD0
LD DE, 1000
CALL .PD0
LD DE, 100
CALL .PD0
LD A, L
JR PD0
.PD0: LD B, 0
.PD1: LD A, H
CP D
JR C, .PD3
JR NZ, .PD2
LD A, L
CP E
JR C, .PD3
.PD2: INC B
SBC HL, DE
JR .PD1
.PD3: LD A, B
ADD '0'
CALL DSPprintChar
RET
; DSP_PRINT_BIN (003Bh)
; Entry ..... A=Value
; Exit ...... None
; Modifies .. AF B
; Print a 8it binary in current cursor position
; ***********************************************
DSPprintBin:
LD B, 8
.PB0: SLA A
PUSH AF
LD A, '0'
JR NC, .PB1
INC A
.PB1: CALL DSPprintChar
POP AF
DJNZ .PB0
RET
; WAIT_FOR_KEY_PRESSED (0050h)
; Entry ..... None
; Exit ...... None
; Modifies .. AF B
; Wait a key be pressed and released removing bouncing
; ******************************************************
waitForKeyPressed:
LD A, 14
OUT (PSG_W_ADDRESS), A
.W0: IN A, (PSG_R_DATA)
AND 01Eh
CP 01Eh
JR Z, .W0
LD B, 0
.W1: DJNZ .W1
.W11: DJNZ .W11
.W12: DJNZ .W12
.W13: DJNZ .W13
.W2: IN A, (PSG_R_DATA)
AND 01Eh
CP 01Eh
JR NZ, .W2
LD B, 0
.W3: DJNZ .W3
.W31: DJNZ .W31
.W32: DJNZ .W32
.W33: DJNZ .W33
RET
ready DB "Ready!!!", 0
loaded DB " bytes loaded", 0
press DB "Press any key...", 0
ORG 1FFFh
NOP