-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtms9918_vga.spin2
634 lines (510 loc) · 21.1 KB
/
tms9918_vga.spin2
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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
{
TMS9918 Emulation
Original VGA Video Driver by Chip Gracey
Tweakes and VDP Emulation by Marco Maccaferri <[email protected]>
}
CON
intensity = 80 '0..128
fpix = 12_500_000
VAR
long status
PUB start(tms_regs, tms_vram, bitmap, pin)
_pn := pin & $38
_tms_status := @status
_tms_regs := tms_regs
_tms_vram := tms_vram
_tms_first := 24
_tms_last := _tms_first + 192
_tms_bitmap := bitmap
_tms_palette := @tms_palette
coginit(16, @driver, 0)
PUB get_status() : rc
if (rc := status) <> 0
status := 0
DAT orgh
tms_palette long $00_00_00_00
long $00_00_00_00
long $66_CC_66_00
long $88_EE_88_00
long $44_44_DD_00
long $77_77_FF_00
long $BB_55_55_00
long $77_DD_DD_00
long $DD_66_66_00
long $FF_77_77_00
long $CC_CC_55_00
long $EE_EE_88_00
long $55_AA_55_00
long $BB_55_BB_00
long $CC_CC_CC_00
long $EE_EE_EE_00
DAT org
'
' Driver
'
driver
setq2 #$10 - 1 'load palette into lut
rdlong 0,_tms_palette
rdlong _xf, #@clkfreq 'calculate streamer frequency
qfrac ##fpix, _xf
getqx _xf
shr _xf, #1
setcmod #%01_0_000_0 'enable colorspace conversion
setcy ##intensity << 24 'r set colorspace for rgb
setci ##intensity << 16 'g
setcq ##intensity << 08 'b
setxfrq _xf 'set transfer frequency
cogid cx 'insert cogid into dac modes
setnib dacmode_s,cx,#2
setnib dacmode_c,cx,#2
wrpin dacmode_s,_pn 'enable 123-ohm 3.3V dac mode in pin +0
xor _pn,#2<<6|1 'enable 75-ohm 2.0V dac mode in pins +1..3
wrpin dacmode_c,_pn
xor _pn,#3<<6 ^(2<<6|1) 'make pins +0..3 outputs
drvl _pn
xor _pn,#4|(3<<6) 'leave av_base pointing to pin +4 (vertical sync)
mov ijmp1,#field 'set up streamer-empty interrupt
setint1 #10
mov scol, #0
xcont #10,#0 'do streamer instruction to start interrupt sequence
'
'
' Main program
'
loop
cmp _cl, scnt wcz
if_z jmp #loop
mov scol, scol+1
mov scnt, _cl
setq #1 ' read registers into temp buffer
rdlong sbuf, _tms_regs ' |
getnib scol+1, sbuf+1, #6 ' border color for next line
movbyts scol+1, #0
cmp _cl, _tms_first wcz ' check if first visible line
if_c jmp #loop
cmp _cl, _tms_last wc ' check if last visible line
if_nc jmp #loop
if_z mov loff, #0 ' if first line reset counters...
if_z mov lcnt, #0
if_z mov ptrv, _tms_bitmap ' ...and pointers
getbyte regs+0, sbuf, #0
test regs+0, #TMSMODE3 wz ' graphics mode II
getbyte regs+1, sbuf, #1
getbyte regs+2, sbuf, #2
shl regs+2, #10 ' name table base address * $400
add regs+2, _tms_vram
getbyte regs+3, sbuf, #3
shl regs+3, #6 ' color table base address * $40
if_nz andn regs+3, vdp_8k_mask ' limit color table to 8k boundaries
add regs+3, _tms_vram
getbyte regs+4, sbuf+1, #0
shl regs+4, #11 ' pattern generator base address * $800
if_nz andn regs+4, vdp_8k_mask ' limit pattern table to 8k boundaries
add regs+4, _tms_vram
getbyte regs+5, sbuf+1, #1
shl regs+5, #7 ' sprite attribute table base address * $80
add regs+5, _tms_vram
getbyte regs+6, sbuf+1, #2
shl regs+6, #11 ' sprite pattern generator base address * $800
add regs+6, _tms_vram
getbyte regs+7, sbuf+1, #3
setq #32-1 ' read all sprite attribute table
rdlong sbuf, regs+5
scanline
mov ptra, ptrv
test regs+1, #TMSBLANK wz ' blank
if_z jmp #blank_line
test regs+1, #TMSMODE1 wz ' text mode
mov a, lcnt
andn a, #$07 ' divide and multiply by 8
mov b, a
shl a, #2 ' multiply by 32
if_nz add a, b ' multiply by 40
add regs+2, a
test regs+1, #TMSMODE2 wz ' multicolor mode
if_nz jmp #multicolor_mode
test regs+1, #TMSMODE1 wz ' text mode
if_z jmp #graphics_mode
text_mode
getnib ctmp, regs+7, #1 ' foreground color
shl ctmp, #8
setnib ctmp, regs+7, #0 ' background color
mov cbuf+0, ctmp
movbyts cbuf+0, #0
setq #1
wrlong cbuf+0, ptra++
mov ptrb, regs+2
mov ecnt, #40
.loop rdbyte ptrc, ptrb++ ' read tile number to display
shl ptrc, #3 ' 8 bytes per tile
add ptrc, regs+4
add ptrc, loff
rdbyte a, ptrc ' pixels, 1 bit per pixel, from msb
rev a
shr a, #24
mergew a
mov cbuf+0, ctmp
movbyts cbuf+0, a
shr a, #8
mov cbuf+1, ctmp
movbyts cbuf+1, a
setq #1
wrlong cbuf+0, ptra++
sub ptra, #2
djnz ecnt, #.loop
mov cbuf+0, ctmp
movbyts cbuf+0, #0
setq #1
wrlong cbuf+0, ptra++
jmp #end_scanline
blank_line
getnib ctmp, regs+7, #0 ' background color
movbyts ctmp, #0
mov ecnt, #256/4 ' fill line with background
.l1 wrlong ctmp, ptra++
djnz ecnt, #.l1
jmp #end_scanline
multicolor_mode
mov a, lcnt
shr a, #2
and a, #$07
mov ptrb, regs+2
mov ecnt, #32
.loop rdbyte ptrc, ptrb++ ' read tile number to display
shl ptrc, #3
add ptrc, regs+4 ' points to color table
add ptrc, a
rdbyte cbuf+0, ptrc ' colors
mov cbuf+1, cbuf+0
and cbuf+1, #$0F wz
if_z setnib cbuf+1, regs+7, #0
shr cbuf+0, #4 wz
if_z setnib cbuf+0, regs+7, #0
movbyts cbuf+0, #%%0000
movbyts cbuf+1, #%%0000
setq #1
wrlong cbuf+0, ptra++
djnz ecnt, #.loop
jmp #sprites
graphics_mode
test regs+0, #TMSMODE3 wz ' graphics mode II
if_nz mov a, lcnt
if_nz shr a, #6 ' divide by 64 (8 rows by 8 pixels)
if_nz shl a, #11 ' multiply by 2048
mov b, loff ' offset into pattern table
if_nz add b, a ' add mode II pattern table offset
bitnz .shlr, #21 ' patch shl(0)/shr(1)
mov ptrb, regs+2
mov ecnt, #32
.loop rdbyte ptrc, ptrb++ ' read tile number to display
mov ptrd, ptrc
.shlr shl ptrd, #3
add ptrd, regs+3 ' points to color table
test regs+0, #TMSMODE3 wz ' graphics mode II
if_nz add ptrd, b
rdbyte a, ptrd ' colors
getnib cbuf+0, a, #0
and cbuf+0, #$0F wz
if_z setnib cbuf+0, regs+7, #0
getnib cbuf+1, a, #1
and cbuf+1, #$0F wz
if_z setnib cbuf+1, regs+7, #0
setbyte cbuf+0, cbuf+1, #1
mov cbuf+1, cbuf+0
shl ptrc, #3 ' 8 bytes per tile
add ptrc, regs+4
add ptrc, b
rdbyte a, ptrc ' pixels, 1 bit per pixel, from msb
rev a
shr a, #24
mergew a
movbyts cbuf+0, a
shr a, #8
movbyts cbuf+1, a
setq #1
wrlong cbuf+0, ptra++
djnz ecnt, #.loop
' fall through
sprites
mov b, #8
test regs+1, #TMSSPRSIZE wz
if_nz shl b, #1
test regs+1, #TMSSPRMAG wz
if_nz shl b, #1
mov ecnt, #14 ' clear buffers
.l1 altd ecnt, #vbuf-1
mov 0-0, #0
djnz ecnt, #.l1
mov stat, #0
mov vcnt, #0
mov ccnt, #0
.loop alts ccnt, #sbuf
mov sprt, 0-0
mov y, sprt
shl y, #24
sar y, #24 ' sign-extend y
cmps y, neg_clip wc
if_c and y, #$FF ' max -32
cmp y, #$D0 wz ' end of sprites processing
if_z jmp #.end
mov a, lcnt ' check sprite scanline visibility
subs a, y wc
if_c jmp #.next
cmp a, b wc
if_nc jmp #.next
setbyte sprt, a, #0
incmod vcnt, #4 wc ' limit sprites on scanline
if_c mov stat, ccnt ' set 5th sprite
if_c or stat, #%010_00000
if_c jmp #.end
altd vcnt, #vbuf-1
mov 0-0, sprt
.next incmod ccnt, #TMSSPRITES-1 wc
if_nc jmp #.loop
.end
mov sprt, vbuf+3 wz
if_nz calld draw_sprite_ret, #draw_sprite
mov sprt, vbuf+2 wz
if_nz calld draw_sprite_ret, #draw_sprite
mov sprt, vbuf+1 wz
if_nz calld draw_sprite_ret, #draw_sprite
mov sprt, vbuf+0 wz
if_nz calld draw_sprite_ret, #draw_sprite
rdbyte a, _tms_status ' update status byte
test stat, #%001_00000 wz
if_nz or a, #%001_00000
test a, #%010_00000 wz
if_z or a, stat
wrbyte a, _tms_status
end_scanline
add ptrv, #256
incmod loff, #7
incmod lcnt, #192-1
jmp #loop
draw_sprite
getbyte a, sprt, #0
mov ptrc, sprt
shr ptrc, #16
and ptrc, #$FF
shl ptrc, #3
add ptrc, regs+6
test regs+1, #TMSSPRMAG wz
if_nz shr a, #1
add ptrc, a
getnib ctmp, sprt, #6 'set sprite color
shl ctmp, #8
getbyte x, sprt, #1
testbn sprt, #31 wz 'TMSEARLYCLK
if_nz sub x, #32
mov ptra, ptrv
add ptra, x
test regs+1, #TMSSPRSIZE wz
if_z mov ecnt, #4
if_nz mov ecnt, #8
rdbyte sprt, ptrc
shl sprt, #8
if_nz add ptrc, #16
if_nz rdbyte a, ptrc
if_nz or sprt, a
test regs+1, #TMSSPRMAG wz
if_nz setword sprt, sprt, #1
if_nz mergew sprt
if_z shl sprt, #16
cmps x, #0 wc ' skip left offscreen pixels
if_c abs x ' |
if_c add ptra, x ' |
if_c sub ecnt, x ' |
if_c cmps x, #32 wz ' |
if_c_and_z mov sprt, #0 ' |
if_c_and_nz shl sprt, x ' |
if_c mov x, #0 ' |
mov cbuf, sprt
mov cbuf+1, cbuf
mov y, x ' check sprite collision
sar y, #5 ' |
add y, #line ' |
setd .c0, y ' |
setd .c0+1, y ' |
add y, #1 ' |
setd .c1, y ' |
setd .c1+1, y ' |
mov a, x ' |
and a, #$1F wz ' |
if_z mov cbuf+1, #0 ' |
shr cbuf, a ' |
.c0 test 0-0, cbuf wz ' |
or 0-0, cbuf ' |
mov y, #32 ' |
sub y, a ' |
shl cbuf+1,y ' |
.c1 if_z test 0-0, cbuf+1 wz ' |
or 0-0, cbuf+1 ' |
if_nz or stat, #%0010_0000 ' update collision flag
mov line+8, #0 ' clear right offscreen pixels
rev sprt
.draw getnib a, sprt, #0
mergew a
mov cbuf+0, ctmp
movbyts cbuf+0, a
wmlong cbuf+0, ptra++
add x, #4
cmp x, #256 wc
shr sprt, #4 wz
if_c_and_nz djnz ecnt, #.draw
jmp draw_sprite_ret
draw_sprite_ret long 0
'
'
' Field loop
'
field
mov cy,#24 'before-visible blank lines
calld border_ret, #border
mov _lb, _tms_bitmap
mov cy,#192 'visible lines
.line xcont _as,#0 'do before-visible part of scan line
add _cl, #1
rdfast #4, _lb 'start reading pixels into fifo
resi1
xcont _bv, scol
resi1
xcont _dv, #0
resi1
xcont _av, scol
resi1
xcont _bs,#0 'do before-sync part of scan line
resi1
xzero _ds,#1 'do sync part of scan line
resi1
' duplicate line
xcont _as,#0 'do before-visible part of scan line
resi1
xcont _bv, scol
resi1
xcont _dv, #0
resi1
xcont _av, scol
resi1
xcont _bs,#0 'do before-sync part of scan line
resi1
xzero _ds,#1 'do sync part of scan line
add _lb, #256
resi1
djnz cy,#.line
rdbyte cy, _tms_status ' update status byte
or cy, #%100_00000 ' vsync flag
wrbyte cy, _tms_status
cogatn #%00000001 'signal vertical sync to cog0
mov cy,#24 'after-visible blank lines
calld border_ret, #border
mov cy, #10
calld blank_ret, #blank
drvnot _pn 'vertical sync on
mov cy, #2
calld blank_ret, #blank
drvnot _pn 'vertical sync off
mov _cl, #0
mov cy, #33
calld blank_ret, #blank
jmp #field 'loop
'
'
' Subroutines
'
blank xcont _as,#0 'do before-visible part of scan line
resi1
xcont _vi,#0 'do visible part of scan line (blank)
resi1
xcont _bs,#0 'do before-sync part of scan line
resi1
xzero _ds,#1 'do sync part of scan line
resi1
djnz cy,#blank 'loop if more blanks needed
jmp blank_ret
blank_ret long 0
border xcont _as,#0 'do before-visible part of scan line
add _cl, #1
resi1
xcont _vi,scol 'do visible part of scan line (blank)
resi1
xcont _bs,#0 'do before-sync part of scan line
resi1
xzero _ds,#1 'do sync part of scan line
resi1
xcont _as,#0 'do before-visible part of scan line
resi1
xcont _vi,scol 'do visible part of scan line (blank)
resi1
xcont _bs,#0 'do before-sync part of scan line
resi1
xzero _ds,#1 'do sync part of scan line
resi1
djnz cy,#border
jmp border_ret
border_ret long 0
'
'
' Data
'
dacmode_s long %0000_0000_000_1011000000000_01_00000_0 'hsync is 123-ohm, 3.3V
dacmode_c long %0000_0000_000_1011100000000_01_00000_0 'R/G/B are 75-ohm, 2.0V
cx long 0
cy long 0
_xf long 0 'streamer frequency setting
_bs long $7F010000+8 'streamer command: before-sync
_ds long $7F010000+48 'streamer command: during-sync
_as long $7F010000+24 'streamer command: after sync
_vi long $3F000000+320 'streamer command: visible
_bv long $3F000000+32 'streamer command: before visible
_dv long $7F080000+256 'streamer command: during visible
_av long $3F000000+32 'streamer command: after visible
_lb long 0 'line buffer pointer
_cl long 0 'current line
_pn long 0 'base pin
_tms_status long 0
_tms_regs long 0
_tms_vram long 0
_tms_first long 0
_tms_last long 0
_tms_bitmap long 0
_tms_palette long 0
vdp_8k_mask long $1FFF
neg_clip long -32
TMSSPRCOLOR long %00001111_00000000_00000000_00000000
a res 1
b res 1
x res 1
y res 1
scnt res 1
scol res 2 'backdrop/border color
lcnt res 1
loff res 1
ccnt res 1
vcnt res 1
ecnt res 1
ctmp res 1
cbuf res 2
sprt res 1
ptrc res 1
ptrd res 1
ptrv res 1
stat res 1
regs res 8
sbuf res 32
vbuf res 4
line res 8
res 1
fit $1F0
CON
TMSMODE3 = %00000010 ' mode bit 3
TMSEXTVID = %00000001 ' external video
' register 1 control bits
TMS4K16K = %10000000 ' 4/16K RAM
TMSBLANK = %01000000 ' screen blank
TMSINTEN = %00100000 ' interrupt enable
TMSMODE1 = %00010000 ' mode bit 1
TMSMODE2 = %00001000 ' mode bit 2
TMSSPRSIZE = %00000010 ' sprite size
TMSSPRMAG = %00000001 ' sprite magnification
TMSVSYNC = %10000000 ' framce sync
TMSSPRITES = 32