-
Notifications
You must be signed in to change notification settings - Fork 0
/
GraphicsII.asm
578 lines (463 loc) · 9.23 KB
/
GraphicsII.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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
.module GraphicsII
Vectors:
jp Execute
jp PutMap
jp BeginPlot
jp SetAlignedHorizontalLineSegment
PatternGenerator = $0000 ; 6KB
ColourTable = $2000 ; 6KB
NameTable = $3800 ; 768 bytes
UserDefinedChars = $3B00 ; 1280 bytes = 160 chars
TopOfMemory = $4000 ; 16KB
.if NameTable % $400
.echoln "Bad address for Graphics II Name Table"
.endif
.if ColourTable % kb(8)
.echoln "Bad address for Graphics II Colour Table Generator"
.endif
.if PatternGenerator % kb(8)
.echoln "Bad address for Graphics II Pattern Generator"
.endif
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 \ ret z
dec a \ ret z
ret
Initialise:
; Switch video mode to TMS9918 GRAPHICS II
ld a,%00000010 ; M3 = 1
ld b,$00
call Video.SetRegister
ld a,%10000000
ld b,$01
call Video.SetRegister
; Name table address
ld a,NameTable / $400
ld b,$02
call Video.SetRegister
; Colour table address
ld a,ColourTable / $40
ld b,$03
call Video.SetRegister
; Patterns generator address
ld a,PatternGenerator / $800
ld b,$04
call Video.SetRegister
; Set background/foreground colour
ld a,$F1 ; TC, BG
ld b,$07
call Video.SetRegister
; Fill name table with sensible data
ld hl,NameTable
call Video.SetWriteAddress
; 768 bytes (3*256) that just count up from 0..255 and loop.
xor a
ld b,a
ld c,3
-: out (Video.Data),a
inc a
djnz -
dec c
jr nz,-
; Reset the colours to sensible defaults.
ld hl,ColourTable
call Video.SetWriteAddress
; 6KB of colour data
ld bc,kb(6)/256
xor a
-: out (Video.Data),a
djnz -
dec c
jr nz,-
; Set up my own vectors!
ld a,$C3 ; JP
ld (Driver.ManipulatePixelColour),a
ld a,$C9 ; RET
ld (Driver.ManipulatePixelBitmask+1),a
ret
; ---------------------------------------------------------
; GetVRAMOffsetForCursor -> gets the offset into VRAM for
; a particular cursor position.
; ---------------------------------------------------------
; Inputs: CurCol, CurRow
; Outputs: hl = offset to VRAM data for tile at (e,a).
; Destroys: af, de, b.
; ---------------------------------------------------------
GetVRAMOffsetForCursor:
ld a,(Console.CurCol)
ld e,a
ld a,(Console.CurRow)
; ---------------------------------------------------------
; GetVRAMOffsetForCursor -> gets the offset into VRAM for
; a particular cursor position.
; ---------------------------------------------------------
; Inputs: a = tile row.
; e = tile column.
; Outputs: hl = offset to VRAM data for tile at (e,a).
; Destroys: af, b.
; ---------------------------------------------------------
GetVRAMOffsetForTile:
; Row *32, *8 = * 256
ld l,a
and %11111000
ld h,a
ld a,0 ; <- optimise
add a,l
and %00000111
or h
ld h,a
ld l,0
; Column *8
ld a,e
add a,a
add a,a
add a,a
ld e,a
ld d,0
add hl,de
ret
PutMap:
push hl
push de
push bc
push af
call GetVRAMOffsetForCursor
pop af ; Get the character number again
push hl ; Save the address offset for later...
; Is it a user-defined character?
cp $80
jr c,ROMFont
; Yes, so read back the user-defined character.
ld hl,8
call Host.GetSafeScratchMemoryHL
jr c,+
call GetUserDefinedCharacter
ex de,hl
jr WriteFontData
+: ld a,' '
ROMFont:
; Get the address of the data in the font.
add a,FontCharOffset
jr c,+
xor a
+: add a,a
ld l,a
ld h,0
add hl,hl
add hl,hl
ld bc,VDU.Fonts.Font8x8
add hl,bc
ex de,hl
WriteFontData:
; Set the write address.
pop hl
push hl
.if PatternGenerator != 0
ld bc,PatternGenerator
add hl,bc
.endif
call Video.SetWriteAddress
; Write the font data
ld b,8
-: ld a,(de) ; 7
out (Video.Data),a ; 11
inc de ; 6
djnz - ; 13 = 37 clocks
; Write the colour data
pop hl
ld de,ColourTable
add hl,de
call Video.SetWriteAddress
ld a,(VDU.Console.Colour)
call VDU.Palettes.ConvertColourPairToTMS9918
ld b,8
-: out (Video.Data),a ; 11
nop ; 4
nop ; 4
djnz - ; 13 = 32 clocks
pop bc
pop de
pop hl
ei
ret
ScrollDown:
push bc
push de
push hl
; A stride of -256 bytes
ld de,-256
; Get the pointer to the bottom left corner in the pattern table.
ld a,(Console.MaxRow)
jr ScrollFromRow
ScrollUp:
push bc
push de
push hl
; A stride of 256 bytes
ld de,256
; Get the pointer to the top left corner in the pattern table.
ld a,(Console.MinRow)
ScrollFromRow:
ld (TempSize),de
ld de,(Console.MinCol)
call GetVRAMOffsetForTile
push hl
.if PatternGenerator != 0
ld de,PatternGenerator
add hl,de
.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
add a,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
push bc
ld de,(TempSize)
call Console.ScrollBlock
pop bc
pop hl
.if ColourTable != 0
ld de,ColourTable
add hl,de
.endif
ld de,(TempSize)
call Console.ScrollBlock
pop hl
pop de
pop bc
ei
ret
BeginPlot:
ld a,(Graphics.PlotShape)
and 3
dec a
jr z,SetForegroundPixel
dec a
jr z,InvertPixel
SetBackgroundPixel:
ld hl,ManipulatePixelColour.SetBackground
ld (Driver.ManipulatePixelColour+1),hl
ld a,$A1 ; AND C
ld (Driver.ManipulatePixelBitmask+0),a
ret
InvertPixel:
ld hl,ManipulatePixelColour.Invert
ld (Driver.ManipulatePixelColour+1),hl
ld a,$A8 ; XOR B
ld (Driver.ManipulatePixelBitmask+0),a
ret
SetForegroundPixel:
ld hl,ManipulatePixelColour.SetForeground
ld (Driver.ManipulatePixelColour+1),hl
ld a,$B0 ; OR B
ld (Driver.ManipulatePixelBitmask+0),a
ret
SetAlignedHorizontalLineSegment:
; IN: (D,E) = (X,Y)
; H = set pixel mask (OR)
; L = clear pixel mask (AND)
push hl
; Character row = Y / 8
; Row *32, *8 = * 256
ld a,e
srl a
srl a
srl a
ld l,a
and %11111000
ld h,a
ld a,0 ; <- optimise
add a,l
and %00000111
or h
ld h,a
; Offset by row value
ld a,e
and %00000111
ld l,a
; Character column = X / 8
; Column / 8, * 8 = truncate
ld a,d
and %11111000
ld c,a
ld b,0
add hl,bc
; Retrieve bitmask
pop bc
; HL -> offset into VRAM to set pixel
push hl ; Store offset
.if PatternGenerator != 0
ld de,PatternGenerator
add hl,de
.endif
call Video.SetReadAddress
in a,(Video.Data)
call Driver.ManipulatePixelBitmask
call Video.SetWriteAddress
out (Video.Data),a
ei
pop hl ; Restore offset into VRAM
.if ColourTable != 0
ld bc,ColourTable
add hl,bc
.endif
call Video.SetReadAddress
in a,(Video.Data)
call Driver.ManipulatePixelColour
call Video.SetWriteAddress
out (Video.Data),a
ei
ret
ManipulatePixelColour.SetForeground:
and %00001111
ld c,a
ld a,(VDU.Graphics.ForegroundColour)
call VDU.Palettes.ConvertPaletteIndexToTMS9918A
rlca
rlca
rlca
rlca
or c
ret
ManipulatePixelColour.SetBackground:
and %11110000
ld c,a
ld a,(VDU.Graphics.BackgroundColour)
call VDU.Palettes.ConvertPaletteIndexToTMS9918A
or c
ret
ManipulatePixelColour.Invert:
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:
add a,a
ret nc ; As we're expecting to map from $80..$FF, must carry
ld l,a
ld h,0
add hl,hl
add hl,hl
ld de,UserDefinedChars
add hl,de
scf
ret
SetUserDefinedCharacter:
ld a,c
push hl
call GetUserDefinedCharacterAddress
pop de
ret nc
call Video.SetWriteAddress
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
ret
+: call Video.SetReadAddress
pop hl
push hl
ld b,8
-: in a,(Video.Data) ; 11
ld (hl),a ; 7
inc hl ; 6
djnz - ; 13 = 37 clocks
pop hl
scf
ei
ret
PreserveUnderCursor:
xor a
jr ManageCursor
RestoreUnderCursor:
ld a,-1
ManageCursor:
ld b,a
call GetVRAMOffsetForCursor
push hl
.if ColourTable != 0
ld de,ColourTable
add hl,de
.endif
pop de ; DE = offset into VRAM
push hl
.if PatternGenerator == 0
push de
.else
ld hl,PatternGenerator
add hl,de
push hl
.endif
ld c,2
ld a,b
or a
jr nz,ManageCursorWrite
ManageCursorRead:
--: pop hl
call Video.SetReadAddress
ld de,VDU.Console.AreaUnderCursor+0
bit 0,c
jr z,+
ld de,VDU.Console.AreaUnderCursor+8
+: ld b,8
-: in a,(Video.Data) ; 11
ld (de),a ; 7
inc de ; 6
djnz - ; 13/8 <- 37
dec c
jr nz,--
ei
ret
ManageCursorWrite:
--: pop hl
call Video.SetWriteAddress
ld de,VDU.Console.AreaUnderCursor+0
bit 0,c
jr z,+
ld de,VDU.Console.AreaUnderCursor+8
+: ld b,8
-: ld a,(de) ; 7
out (Video.Data),a ; 11
inc de ; 6
djnz - ; 13/8 <- 37
dec c
jr nz,--
ei
ret
.endmodule