This repository was archived by the owner on Feb 10, 2025. It is now read-only.
forked from jblang/bbcbasic-z80
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhardware.asm
More file actions
286 lines (257 loc) · 5.93 KB
/
hardware.asm
File metadata and controls
286 lines (257 loc) · 5.93 KB
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
; Copyright (c) 2020 J.B. Langston
;
; This software is provided 'as-is', without any express or implied
; warranty. In no event will the authors be held liable for any damages
; arising from the use of this software.
;
; Permission is granted to anyone to use this software for any purpose,
; including commercial applications, and to alter it and redistribute it
; freely, subject to the following restrictions:
;
; 1. The origin of this software must not be misrepresented; you must not
; claim that you wrote the original software. If you use this software
; in a product, an acknowledgment in the product documentation would be
; appreciated but is not required.
; 2. Altered source versions must be plainly marked as such, and must not be
; misrepresented as being the original software.
; 3. This notice may not be removed or altered from any source distribution.
; HARDWARE FUNCTIONS
include "hbios.inc"
include "constants.inc"
VDPPORT EQU $BE ; default port
PUBLIC COLOUR
PUBLIC ENVEL
PUBLIC ADVAL
PUBLIC POINT
PUBLIC GETIMS
PUBLIC PUTIMS
PUBLIC CLRSCN
PUBLIC PUTCSR
PUBLIC GETCSR
EXTERN EXTERR
EXTERN TELL
EXTERN PBCDL
EXTERN OUTCHR
EXTERN EXPRI
EXTERN COMMA
EXTERN XEQ
EXTERN XEQ0
EXTERN GETIME
EXTERN PUTIME
EXTERN SOUND_INIT
PUBLIC HBIOS_INIT
HBIOS_INIT:
; Using default ports for moment - version 3.1.pre.15 does not support this HBIOS call
; PUSH AF ; Retrieve TMS9918 Ports
; CALL TELL
; DEFM "Retriving ports from HBIOS"
; DEFB CR
; DEFB LF
; DEFB 0
; LD B, VDAIO
; LD C, 0
; RST HBIOS
; LD C, D
; LD B, 0
; LD (VDPADR), BC
CALL SOUND_INIT
; POP AF
RET
;CLRSCN - Clear screen.
; (Alter characters to suit your VDU)
; Destroys: A,D,E,H,L,F
CLRSCN: PUSH BC
CALL TELL
DEFB ESC,"[;H" ; home cursor
DEFB ESC,"[2J" ; clear screen
DEFB 0
LD BC,0
LD (TEXTX),BC
LD (TEXTY),BC
POP BC
RET
;PUTCSR - Move cursor to specified position.
; Inputs: DE = horizontal position (LHS=0)
; HL = vertical position (TOP=0)
; Destroys: A,D,E,H,L,F
PUTCSR: LD (TEXTX),DE ;SAVE X & Y FOR LATER
LD (TEXTY),HL
LD A,ESC
CALL OUTCHR
LD A,'['
CALL OUTCHR
LD DE,(TEXTX)
CALL PBCDL
LD A,';'
CALL OUTCHR
LD HL,(TEXTY)
CALL PBCDL
LD A,'H'
CALL OUTCHR
RET
;GETCSR - Return cursor coordinates.
; Outputs: DE = X coordinate (POS)
; HL = Y coordinate (VPOS)
; Destroys: A,D,E,H,L,F
GETCSR: LD DE,(TEXTX)
LD HL,(TEXTY)
RET
;
;COLOUR - Set text color
COLOUR: CALL EXPRI
EXX
LD A,L
AND 7
BIT 7,L
JP Z,FGCLR
ADD 10
FGCLR: ADD 30
BIT 3,L
JP Z,OUTCLR
ADD 60
OUTCLR: LD L,A
LD A,ESC
CALL OUTCHR
LD A,'['
CALL OUTCHR
CALL PBCDL
LD A,'m'
CALL OUTCHR
JP XEQ
; unimplemented functions
POINT:
ENVEL:
ADVAL:
GETIMS:
PUTIMS:
XOR A
CALL EXTERR
DEFM "Not implemented"
DEFB 0
; VDP state
VDPADR: DEFB VDPPORT ; VDP base port
VDPINT: DEFB 0 ; 0 = NMI, 1 = INT
CMODE: DEFB 2 ; Current graphics mode
CCOLOR: DEFB 0F0H ; Current text color
GRAPHX: DEFW 0 ; Current graphics X coordinate
GRAPHY: DEFW 0 ; Current graphics Y coordinate
TEXTX: DEFW 0 ; Current text X coordinate
TEXTY: DEFW 0 ; Current text Y coordinate
; VDP table addresses
NAMTAB EQU 3800H ; name table
COLTAB EQU 2000H ; color table
PATTAB EQU 0H ; pattern table
SPATAB EQU 3BC0H ; sprite attribute table
SPPTAB EQU 1800H ; sprite pattern table
; default register values
DEFREG: DEFB 0, 80H ; blank, 16KB enabled
DEFB NAMTAB/400H
DEFB COLTAB/40H ; calculate register values from
DEFB PATTAB/800H ; addresses defined above
DEFB SPATAB/80H
DEFB SPPTAB/800H
; mode-specific register values
MODREG: DEFB 0, 0D0H ; MODE 0: text
DEFB 0, 0C0H ; MODE 1: tile graphics
DEFB 2, 0C0H ; MODE 2: bitmap graphics
DEFB 0, 0C8H ; MODE 3: multicolor graphics
; initialize vdp registers to default values
; A = mode to set
VDPINI:
AND 3 ; limit to modes 0-3
LD (CMODE),A ; save mode for later
LD BC,(VDPADR) ; send to vdp
INC C ; select vdp register port
LD HL,DEFREG ; look up default register values
LD B,7 ; register counter
LD D,80H ; register address
REGLP: LD E,(HL)
OUT (C),E
OUT (C),D
INC HL
INC D
DJNZ REGLP
LD A,(CCOLOR) ; restore saved colors
OUT (C),A
OUT (C),D
; clear vram
LD DE,4000H ; 16KB of memory
OUT (C),E ; write at start of memory
OUT (C),D
DEC C ; select vram port
CLRLP: OUT (C),B
DEC DE
LD A,D
OR E
JP NZ,CLRLP
; mode-specific initialization
LD A,(CMODE)
CP 3
JP Z,MODE3
CP 2
JP NZ,SETREG
; initialize bitmap graphics (MODE 2)
INC C ; select vdp register port
LD B,83H ; color table register
LD A,0FFH ; color table at 2000H
OUT (C),A
OUT (C),B
LD B,84H ; pattern table register
LD A,3 ; pattern table at 0H
OUT (C),A
OUT (C),B
LD DE,NAMTAB ; write to name table
SET 6,D
OUT (C),E
OUT (C),D
DEC C ; select vram port
LD B,3 ; initialize name table with 3 sets
XOR A ; of 256 bytes ranging from 00-FF
M2LOOP: OUT (C),A
NOP ; extra time to finish vram write
INC A
JP NZ,M2LOOP
DJNZ M2LOOP
JP SETREG
; initialize multicolor graphics (MODE 3)
MODE3: INC C ; select vdp register port
LD DE,NAMTAB ; write to name table
SET 6,D
OUT (C),E
OUT (C),D
DEC C ; select vram port
XOR A ; first section starts at 0
LD D,6 ; nametable has 6 different sections
M3LP1: LD E,4 ; each section has 4 identical lines
M3LP2: PUSH AF ; save line starting value
LD B,32 ; each line is 32 bytes long
M3LP3: OUT (C),A
NOP ; extra time to finish vram write
INC A
DJNZ M3LP3
POP AF ; recover line starting value
DEC E ; line counter
JP NZ,M3LP2
ADD A,32 ; increase line's starting value
DEC D ; section counter
JP NZ,M3LP1
; set mode specific registers
SETREG: LD A,(CMODE)
LD D,0
LD E,A
LD HL,MODREG ; look up mode register values
ADD HL,DE
ADD HL,DE
INC C ; select vdp register port
LD A,(HL) ; send mode register 1
LD B,80H
OUT (C),A
OUT (C),B
INC HL ; send mode register 2
LD A,(HL)
INC B
OUT (C),A
OUT (C),B
RET
; Bit mask values for X coordinates 0 through 7
MASK: DEFB 80H, 40H, 20H, 10H, 8H, 4H, 2H, 1H