-
Notifications
You must be signed in to change notification settings - Fork 0
/
MasterSystem16ColoursReduced.asm
395 lines (312 loc) · 6.25 KB
/
MasterSystem16ColoursReduced.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
.module MasterSystem16ColoursReduced
UserDefinedChars = MasterSystem16Colours.NameTable - 128 * 8
Vectors:
jp Execute
jp PutMap
jp MasterSystem16Colours.BeginPlot
jp MasterSystem16Colours.SetAlignedHorizontalLineSegment
Execute:
or a \ jr z,Initialise
dec a \ ret z
dec a \ ret z
dec a \ jp z,ScrollDown
dec a \ jp z,ScrollUp
dec a \ jp z,GetUserDefinedCharacter
dec a \ jp z,SetUserDefinedCharacter
dec a \ jp z,PreserveUnderCursor
dec a \ jp z,RestoreUnderCursor
dec a \ jp z,MasterSystem16Colours.SelectPalette
dec a \ jp z,VDU.ResetMasterSystemPalette
dec a \ jp z,ResetConsoleViewport
dec a \ jp z,ResetGraphicsViewport
dec a \ jp z,MasterSystem16Colours.GetPixel
ret
Initialise:
call MasterSystem16Colours.Initialise
ld hl,Graphics.DivideBy8
ld (Graphics.DivideCoordinate+1),hl
ld hl,Graphics.MultiplyBy8
ld (Graphics.MultiplyCoordinate+1),hl
; Fill the nametable with graphics tiles.
ld hl,MasterSystem16Colours.NameTable+(6+4*32)*2
ld de,MasterSystem16Colours.MinGraphicsTile
ld c,16
--: push bc
ld b,20
call Video.SetWriteAddress
-: ld a,e ; 4
out (Video.Data),a ; 11
ld a,d ; 4
push hl ; 11
pop hl ; 10
out (Video.Data),a ; 11
inc de ; 6
djnz - ; 12
ld bc,32*2
add hl,bc
pop bc
dec c
jr nz,--
; Clear the pattern generator.
ld hl,MasterSystem16Colours.PatternGenerator+MasterSystem16Colours.MinGraphicsTile*32
call Video.SetWriteAddress
ld bc,20*16*32
-: xor a
out (Video.Data),a
dec bc
ld a,b
or c
jr nz,-
ret
ResetConsoleViewport:
; Resolution = 20 * 16
ld a,6
ld (Console.MinCol),a
ld (Console.OriginX),a
ld a,25
ld (Console.MaxCol),a
ld a,20
ld (Console.MaxWidth),a
ld a,4
ld (Console.MinRow),a
ld (Console.OriginY),a
ld a,19
ld (Console.MaxRow),a
ld a,16
ld (Console.MaxHeight),a
ret
ResetGraphicsViewport:
; Resolution = 160 * 128
ld a,48
ld (Graphics.MinX),a
ld (Graphics.OffsetX),a
ld a,255-48
ld (Graphics.MaxX),a
ld a,32
ld (Graphics.MinY),a
ld (Graphics.OffsetY),a
ld a,191-32
ld (Graphics.MaxY),a
ret
PutMap:
cp 32
ret c
push hl
push de
push bc
call VDU.GetCharacterData
push hl
call GetPatternAddressForCursor
call Video.SetWriteAddress
pop hl
ld a,(Console.Colour)
ld d,a
rrca
rrca
rrca
rrca
ld e,a
ld c,8
--: push de
ld b,4
-: push bc
ld a,(hl)
cpl
srl e
jr c,+
xor a
+: ld c,a
srl d
sbc a,a
and (hl)
or c
out (Video.Data),a
pop bc
djnz -
inc hl
pop de
dec c
jr nz,--
pop bc
pop de
pop hl
ei
ret
GetPatternAddressForCursor:
call MasterSystem16Colours.GetNameTableAddressForCursor
call Video.SetReadAddress
in a,(Video.Data)
ld l,a
push hl
pop hl
in a,(Video.Data)
ld h,a
ld b,5
-: add hl,hl
djnz -
.if MasterSystem16Colours.PatternGenerator != 0
ld bc,MasterSystem16Colours.PatternGenerator
add hl,bc
.endif
ret
PreserveUnderCursor:
call GetPatternAddressForCursor
ld de,MasterSystem16Colours.PatternGenerator+32
call CopyPatternData
ei
ret
RestoreUnderCursor:
call GetPatternAddressForCursor
call Video.SetWriteAddress
ld de,MasterSystem16Colours.PatternGenerator+32
ex de,hl
call CopyPatternData
ei
ret
CopyPatternData:
call Video.SetReadAddress
ld hl,32
call Host.GetSafeScratchMemoryHL
ret c
push hl
ld b,32
-: in a,(Video.Data)
ld (hl),a
inc hl
djnz -
pop hl
ex de,hl
call Video.SetWriteAddress
ld b,32
-: ld a,(de)
out (Video.Data),a
inc de
djnz -
ret
ScrollDown:
push bc
push de
push hl
; Each row is 64 bytes long.
ld de,-64
; Get the pointer to the bottom left corner.
ld a,(Console.MaxRow)
jr ScrollFromRow
ScrollUp:
push bc
push de
push hl
; Each row is 64 bytes long.
ld de,64
; Get the pointer to the top left corner.
ld a,(Console.MinRow)
ScrollFromRow:
call MasterSystem16Colours.AMul64
ld a,(Console.MinCol)
add a,a
ld c,a
ld b,0
add hl,bc
.if MasterSystem16Colours.NameTable != 0
ld bc,MasterSystem16Colours.NameTable
add hl,bc
.endif
; How many columns will we need to move?
ld a,(Console.MinCol)
ld c,a
ld a,(Console.MaxCol)
sub c
inc a
add a,a
ld c,a
; How many rows will we need to move?
ld a,(Console.MinRow)
ld b,a
ld a,(Console.MaxRow)
sub b
inc a
ld b,a
call Console.RotateBlock
; Clear the bottom row by temporarily setting a viewport where minrow = maxrow.
ld bc,(Console.MinRow)
push bc
bit 7,d
jr z,+
ld b,c
jr ++
+: ld c,b
++:
ld (Console.MinRow),bc
call Console.Clear
pop bc
ld (Console.MinRow),bc
pop hl
pop de
pop bc
ei
ret
; ---------------------------------------------------------
; GetUserDefinedCharacterAddress -> Gets address in VRAM
; ---------------------------------------------------------
; Inputs: a = character to get the address of.
; Outputs: nc if the character is out of range.
; hl = address of the character otherwise.
; Destroys: af, de.
; ---------------------------------------------------------
; ==========================================================================
; GetUserDefinedCharacterAddress
; --------------------------------------------------------------------------
; Gets address in VRAM.
; --------------------------------------------------------------------------
; Inputs: A: Character to get the address of.
; Outputs: F: NC if the character is out of range, C if character is in range
; HL: Address of the character if it's in range.
; Destroyed: F, DE.
; ==========================================================================
GetUserDefinedCharacterAddress:
add a,a
ret nc
ld h,0
ld l,a
add hl,hl
add hl,hl
ld de,UserDefinedChars
add hl,de
scf
ret
SetUserDefinedCharacter:
push hl
ld a,c
call GetUserDefinedCharacterAddress
jr c,+
pop hl
jp MasterSystem16Colours.SetUserDefinedCharacter
+: call Video.SetWriteAddress
pop de
ld b,8
-: ld a,(de) ; 7
out (Video.Data),a ; 11
inc de ; 6
djnz - ; 13 = 37 clocks
ei
ret
GetUserDefinedCharacter:
push hl
ld a,c
call GetUserDefinedCharacterAddress
jr c,+
pop hl
jp MasterSystem16Colours.GetUserDefinedCharacter
+: call Video.SetReadAddress
ld b,8
pop hl
push hl
-: in a,(Video.Data) ; 11
ld (hl),a ; 7
inc hl ; 6
djnz - ; 13 = 37 clocks
pop hl
scf
ei
ret
.endmodule