-
Notifications
You must be signed in to change notification settings - Fork 0
/
TMS9918.spin
1329 lines (1201 loc) · 101 KB
/
TMS9918.spin
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
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{{
-----------------------------------------------------------------------------------------------
Object to interface with TMS9918/19/28/29 Video Display Processor (or F18A FPGA implementation)
-----------------------------------------------------------------------------------------------
Author: [email protected] (use freely, but give credit, that's what it boils down to)
-----------------------------------------------------------------------------------------------
This object was developed based on following documentation:
https://github.com/cbmeeks/TMS9918
http://codehackcreate.com/archives/30
-----------------------------------------------------------------------------------------------
It has been tested on F18A, but not real TMS99x8/9 family. In order to work on retro-hardware,
modifications may be needed, most notably delays in the read/update/write cycle (consuming the
line scan interrupt to make the changes only during scan times outside of visible area. F18A
has much higher memory bandwith and no such limitations.
----------------------------------------------------------------------------------------------
Version Date Notes
----------------------------------------------------------------------------------------------
0.91 2017-07-04 Added basic sprite support, and flags to turn on/off interrupt
wait and console logging. GRAPHICS2 mode still blows up... :-(
0.90 2017-06-03 Basic text and graphics primities, no sprites. GRAPHICS2 mode
still has a bug that is causing random scrambling of the display
after some number of drawing operations are executed
----------------------------------------------------------------------------------------------
See demo video here: https://youtu.be/FW8V7gS8_GI
----------------------------------------------------------------------------------------------
}}
CON
_clkmode = xtal1 + pll16x 'Standard clock mode * crystal frequency = 80 MHz
_xinfreq = 5_000_000
'_clkmode = xtal3 + pll4x 'high speed crystal multiplied by 4 = 85.90908MHz
'_xinfreq = 21_477_270 'V9958 crystal
CON
STACK_LEN = 64
CON
'Signal Propeller pin VDP pin ( == F18A pins)
nRESET = 27'12' 34 == pull low for reset
MODE = 26'11' 13 == memory/register mode
nCSW = 25'10' 14 == write to register or VDP memory
nCSR = 24'9' ' 15 == read from register or VDP memory
nINT = 23'8' 16 == input always, activated after each scan line if enabled
CD0 = 7' 24 == MSB (to keep with "reverse" TMS99XX family documentation)
CD1 = 6' 23
CD2 = 5' 22
CD3 = 4' 21
CD4 = 3' 20
CD5 = 2' 19
CD6 = 1' 18
CD7 = 0' 17 == LSB
'VSS 12 == GND
'VCC 33 == +5V
'
' Colors
TRANSPARENT = 0
BLACK = 1
MEDIUMGREEN = 2
LIGHTGREEN = 3
DARKBLUE = 4
LIGHTBLUE = 5
DARKRED = 6
CYAN = 7
MEDIUMRED = 8
LIGHTRED = 9
DARKYELLOW = $A
LIGHTYELLOW = $B
DARKGREEN = $C
MAGENTA = $D
GRAY = $E
WHITE = $F
' Video modes
GRAPHICS1 = $0
GRAPHICS2 = $1
MULTICOLOR = $2
TEXT = $3
' Sprite modes
SPRITESIZE_8X8 = %0000_0000
SPRITESIZE_16X16 = %0000_0010
SPRITEMAGNIFICATION_NONE = %0000_0000
SPRITEMAGNIFICATION_2X = %0000_0001
' SetSprite() masks allow fine-grained updates
SPRITEMASK_SETPATTERN = $01
SPRITEMASK_SETCOLOR = $02
SPRITEMASK_SETX = $04 'will override SPRITEMASK_DX
SPRITEMASK_SETY = $08 'will override SPRITEMASK_DY
SPRITEMASK_DX = $10
SPRITEMASK_DY = $20
SPRITEMASK_VX = $40
SPRITEMASK_VY = $80
' Commands, 0 params
CMD_NOOP = $00
CMD_BLANK = $01
CMD_DISPLAY = $02
CMD_RESET = $03
CMD_HOME = $04
CMD_CLS = $05
' Commands, 1 param
CMD_SETMODE = $10
CMD_WRITETEXT = $11
CMD_SETSPRITEMODE = $12
' Commands, 2 param
CMD_SETCOLORS = $20
' Commands, 3 param
CMD_DRAWPIXEL = $30
CMD_READMEM = $31
CMD_WRITEMEM = $32
CMD_SETSPRITEPATTERN = $34
' Commands, 4 param
CMD_FILLMEM = $40
CMD_DRAWCIRCLE = $41
' Commands, 5 param
CMD_DRAWTEXT = $50
CMD_DRAWLINE = $51
' Commands, 6 param
CMD_SETSPRITE = $60
'Some ASCII codes with special handling during WriteText
CS = 16 ''CS: Clear Screen
HM = 1 ''HM: HoMe cursor
NL = 13 ''NL: New Line
CR = 13 ''CR: Carriage return == NL
LF = 10 ''LF: Line Feed
ML = 3 ''ML: Move cursor Left
MR = 4 ''MR: Move cursor Right
MU = 5 ''MU: Move cursor Up
MD = 6 ''MD: Move cursor Down
TB = 9 ''TB: TaB
BS = 8 ''BS: BackSpace
VAR
long stack[STACK_LEN]
byte lockCommandBuffer
byte cogCurrent
byte reg[8] '"shadow" registers for read access convenience, as the VDP only allows writing to them
long plCommand
long lastScanCnt
byte lastStatus
long vdpAccessWindow
long skipTrace
LONG displayMode
LONG nextCharRow, nextCharCol
LONG lastCharRow, lastCharCol
LONG lastPixY, lastPixX
LONG lastSpriteY, lastSpriteX
BYTE colorGraphicsForeAndBack
WORD spriteSpeed[32]
LONG lastSpritePositionUpdateCnt[32]
OBJ
pst : "Parallax Serial Terminal"
PUB Start(plCommandBuffer, initialMode, useInterrupt, enableTracing) : success
longfill(@stack, 0, STACK_LEN)
skipTrace := true
if (enableTracing)
pst.Start(115_200)
pst.Clear
skipTrace := false
Stop
plCommand := plCommandBuffer
longfill(@spriteSpeed, 0, 32)
colorGraphicsForeAndBack := byte[@GoodContrastColorsTable]
_prompt(String("Press any key to continue with TMS9918 object start using command buffer at "), plCommand)
lockCommandBuffer := locknew
if (lockCommandBuffer == -1)
_logError(String("No locks available to start object!"))
return false
else
cogCurrent := cognew(_vdpProcess(initialMode, useInterrupt), @stack)
if (cogCurrent == -1)
_logError(String("No cogs available to start object!"))
lockret(lockCommandBuffer~)
return false
waitcnt((clkfreq * 1) + cnt)
_logTrace(String("TMS9918 object launched into cog "), cogCurrent, String(" using lock "), lockCommandBuffer, String(" at clkfreq "), clkfreq, 0)
return true
PUB Stop
if lockCommandBuffer <> -1
_logTrace(String("Returning TMS9918 object lock "), lockCommandBuffer, 0, 0, 0, 0, 0)
lockret(lockCommandBuffer~)
if cogCurrent > 0
_logTrace(String("Stopping TMS9918 object cog "), cogCurrent, 0, 0, 0, 0, 0)
cogstop(cogCurrent~)
PRI _vdpProcess(initialMode, useInterrupt) |i, y, timer
_logTrace(String("TMS9918 object starting in cog "), cogId, String(" using lock "), lockCommandBuffer, String(" at clkfreq "), clkfreq, 0)
nextCharRow := 0
nextCharCol := 0
if (useInterrupt)
vdpAccessWindow := ((((clkfreq / 60) * (262 - 192)) / 262) * 95) / 100 'see table 3.3 in TMS9918 documentation (we have 70 scan lines every 1/60s)
else
vdpAccessWindow := clkfreq / 60
_logTrace(String("Initial mode is "), initialMode, String(" use interrupt is "), useInterrupt, String(" vdp access clock cycles is "), vdpAccessWindow, 0)
outa[nReset .. CD7]~~ 'set all to 1 (inactive)
dira[nReset .. CD7]~ 'set all to input first
dira[nReset .. nCSR]~~ 'these are always outputs
_vdpReset
_setReg(1, reg[1] & %1011_1111) 'blank screen
lastStatus := _readStatus
_fillVdpMem(0, 16 * 1024, 170, 0) '10101010 pattern
'this is the first command that will be executed
long[plCommand][0] := CMD_SETMODE
long[plCommand][1] := initialMode
displayMode := initialMode
longfill(@lastSpritePositionUpdateCnt, cnt, 32)
repeat 'keep executing commands until cog is stopped
repeat until not lockset(lockCommandBuffer) 'wait for the free lock (don't execute while command buffer is updated)
'update position of even numbered sprites according to their speed, if set
_updateSpritePositions(0)
timer := cnt
case LONG[plCommand]
CMD_SETSPRITEMODE:
_setSpriteMode(long[plCommand][1] & %0000_0011)
'_logCommand(String("CMD_SETSPRITEMODE in mode "), _interval(cnt, timer))
CMD_SETSPRITEPATTERN:
_copyToVdpMem(SpritePatternTable + (long[plCommand][1] << 3), long[plCommand][2], long[plCommand][3])
'_logCommand(String("CMD_SETSPRITE in mode "), _interval(cnt, timer))
CMD_SETSPRITE:
_setSprite(long[plCommand][1], long[plCommand][2], long[plCommand][3], long[plCommand][4], long[plCommand][5], long[plCommand][6])
'_logCommand(String("CMD_SETSPRITE in mode "), _interval(cnt, timer))
CMD_HOME:
_homeTextScreen
'_logCommand(String("CMD_HOME in mode "), _interval(cnt, timer))
CMD_CLS:
case displayMode
GRAPHICS1, TEXT:
_homeTextScreen
_clearTextScreen
GRAPHICS2:
_fillVdpMem(PatternTable, (256 * 192) / 8, 0, 0)
MULTICOLOR:
_fillVdpMem(PatternTable, (64 * 48) / 2, (colorGraphicsForeAndBack << 4) | (colorGraphicsForeAndBack & $F), 0)
'_logCommand(String("CMD_CLS in mode "), _interval(cnt, timer))
CMD_FILLMEM:
_fillVdpMem(long[plCommand][1], long[plCommand][2], long[plCommand][3], long[plCommand][4])
'_logCommand(String("CMD_FILLMEM in mode "), _interval(cnt, timer))
CMD_READMEM:
_copyFromVdpMem(long[plCommand][1], long[plCommand][2], long[plCommand][3])
'_logCommand(String("CMD_READMEM in mode "), _interval(cnt, timer))
CMD_WRITEMEM:
_copyToVdpMem(long[plCommand][1], long[plCommand][2], long[plCommand][3])
'_logCommand(String("CMD_WRITEMEM in mode "), _interval(cnt, timer))
CMD_DRAWPIXEL:
_drawPixel(long[plCommand][1], long[plCommand][2], long[plCommand][3])
'_logCommand(String("CMD_DRAWPIXEL in mode "), _interval(cnt, timer))
CMD_DRAWLINE:
_drawLine(long[plCommand][1], long[plCommand][2], long[plCommand][3], long[plCommand][4], long[plCommand][5])
'_logCommand(String("CMD_DRAWLINE in mode "), _interval(cnt, timer))
CMD_DRAWCIRCLE:
_drawCircle(long[plCommand][1], long[plCommand][2], long[plCommand][3], long[plCommand][4])
'_logCommand(String("CMD_DRAWCIRCLE in mode "), _interval(cnt, timer))
CMD_DRAWTEXT:
_drawText(long[plCommand][1], long[plCommand][2], long[plCommand][3], long[plCommand][4], long[plCommand][5])
'_logCommand(String("CMD_DRAWTEXT in mode "), cnt - timer)
CMD_WRITETEXT:
_writeText(long[plCommand][1])
'_logCommand(String("CMD_WRITETEXT in mode "), _interval(cnt, timer))
CMD_SETCOLORS:
_setReg(7, long[plCommand][2])
'in case this is called before SET_MODE, overwrite default reg7 for all modes
byte[@Mode_Graphics1][7] := reg[7]
byte[@Mode_Graphics2][7] := reg[7]
byte[@Mode_Multicolor][7] := reg[7]
byte[@Mode_Text][7] := reg[7]
colorGraphicsForeAndBack := long[plCommand][1]
case displayMode
GRAPHICS1:
_fillVdpMem(ColorTable, 32, colorGraphicsForeAndBack, 0)
GRAPHICS2:
_fillVdpMem(ColorTable, (256 * 192) / 8, colorGraphicsForeAndBack, 0)
_logCommand(String("CMD_SETCOLORS in mode "), _interval(cnt, timer))
CMD_SETMODE:
displayMode := long[plCommand][1]
case displayMode
GRAPHICS1:
_initialize(32, 24, 0, 0, 256, 192, @Mode_Graphics1, useInterrupt)
_initCharTable(PatternTable, false)
_fillVdpMem(ColorTable, 32, colorGraphicsForeAndBack, 0)
_homeTextScreen
_clearTextScreen
_setReg(1, reg[1] | %0100_0000) 'show screen
'_logTrace2(String("Name table: "), NameTable, String(" Pattern table: "), PatternTable, String(" Color table: "), ColorTable, 8)
_logCommand(String("CMD_SETMODE to mode "), _interval(cnt, timer))
GRAPHICS2:
_initialize(0, 0, 256, 192, 256, 192, @Mode_Graphics2, useInterrupt)
_fillVdpMem(PatternTable, (256 * 192) / 8, 0, 0)
_fillVdpMem(ColorTable, (256 * 192) / 8, colorGraphicsForeAndBack, 0)
_fillVdpMem(NameTable + 000, 256, 0, 1)
_fillVdpMem(NameTable + 256, 256, 0, 1)
_fillVdpMem(NameTable + 512, 256, 0, 1)
_setReg(1, reg[1] | %0100_0000) 'show screen
'_logTrace2(String("Name table: "), NameTable, String(" Pattern table: "), PatternTable, String(" Color table: "), ColorTable, 8)
_logCommand(String("CMD_SETMODE to mode "), _interval(cnt, timer))
MULTICOLOR:
_initialize(0, 0, 64, 48, 256, 192, @Mode_Multicolor, useInterrupt)
_fillVdpMem(PatternTable, (64 * 48) / 2, (colorGraphicsForeAndBack << 4) | (colorGraphicsForeAndBack & $0F), 0)
repeat y from 0 to 23
_fillVdpMem(NameTable + (y << 5), 32, 32 * (y >> 2), 1)
_setReg(1, reg[1] | %0100_0000) 'show screen
'_logTrace2(String("Name table: "), NameTable, String(" Pattern table: "), PatternTable, String(" Color table: "), ColorTable, 8)
_logCommand(String("CMD_SETMODE to mode "), _interval(cnt, timer))
TEXT:
_initialize(40, 24, 0, 0, 0, 0, @Mode_Text, useInterrupt)
_initCharTable(PatternTable, true)
_homeTextScreen
_clearTextScreen
_setReg(1, reg[1] | %0100_0000) 'show screen
'_logTrace2(String("Name table: "), NameTable, String(" Pattern table: "), PatternTable, String(" Color table: "), ColorTable, 8)
_logCommand(String("CMD_SETMODE to mode "), _interval(cnt, timer))
other:
_logError(String("Not a valid video mode."))
CMD_BLANK:
_setReg(1, reg[1] & %1011_1111)
_logCommand(String("CMD_BLANK in mode "), _interval(cnt, timer))
CMD_DISPLAY:
_setReg(1, reg[1] | %0100_0000)
_logCommand(String("CMD_DISPLAY in mode "), _interval(cnt, timer))
CMD_RESET: 'Definitely call SetMode() after this command!
_vdpReset
_logCommand(String("CMD_RESET in mode "), _interval(cnt, timer))
CMD_NOOP:
long[plCommand] := CMD_NOOP
'update position of odd numbered sprites according to their speed, if set
_updateSpritePositions(1)
lockclr(lockCommandBuffer)
{{
Commands to be executed by VDP. Note that these execute in the calling cog, and the parameters are copied over to a common
main memory buffer to be picked up by the cog driving the VDP. A single lock (mutex) is used to avoid the VDP cog to start
consuming half-copied command buffer. This could be expanded into a deeper FIFO to further parallelize visual command exe-
cution (right now caller cog will wait for VDP cog before executing next command but otherwise it will proceed with other
work).
}}
PUB Reset
_setCommand(CMD_RESET, 0, 0, 0, 0, 0, 0, 0)
PUB Display
_setCommand(CMD_DISPLAY, 0, 0, 0, 0, 0, 0, 0)
PUB Blank
_setCommand(CMD_BLANK, 0, 0, 0, 0, 0, 0, 0)
PUB Cls
_setCommand(CMD_CLS, 0, 0, 0, 0, 0, 0, 0)
PUB Home
_setCommand(CMD_HOME, 0, 0, 0, 0, 0, 0, 0)
PUB DrawText(pbText, columnLeft, rowTop, columnRight, rowBottom)
_setCommand(CMD_DRAWTEXT, pbText, columnLeft, rowTop, columnRight, rowBottom, 0, 0)
PUB WriteText(pbText)
_setCommand(CMD_WRITETEXT, pbText, 0, 0, 0, 0, 0, 0)
PUB SetColors(colorsGraphics, colorsText)
_setCommand(CMD_SETCOLORS, colorsGraphics, colorsText, 0, 0, 0, 0, 0)
PUB SetMode(vdpMode)
_setCommand(CMD_SETMODE, vdpMode, 0, 0, 0, 0, 0, 0)
PUB DrawPixel(x, y, color)
_setCommand(CMD_DRAWPIXEL, x, y, color, 0, 0, 0, 0)
PUB DrawLine(xs, ys, xe, ye, color)
_setCommand(CMD_DRAWLINE, xs, ys, xe, ye, color, 0, 0)
PUB DrawCircle(xc, yc, radius, color)
_setCommand(CMD_DRAWCIRCLE, xc, yc, radius, color, 0, 0, 0)
PUB FillMem(pbVdp, count, value, increment)
_setCommand(CMD_FILLMEM, pbVdp, count, value, increment, 0, 0, 0)
PUB ReadMem(pbVdp, pbMain, count)
_setCommand(CMD_READMEM, pbVdp, pbMain, count, 0, 0, 0, 0)
PUB WriteMem(pbVdp, pbMain, count)
_setCommand(CMD_WRITEMEM, pbVdp, pbMain, count, 0, 0, 0, 0)
PUB SetSpriteMode(spriteMode)
_setCommand(CMD_SETSPRITEMODE, spriteMode, 0, 0, 0, 0, 0, 0)
PUB SetSpritePattern(patternId, patternAddr, patternLen)
if (patternId > 255)
_logError(String("Invalid patternId in SetSpritePattern()."))
else
if (patternLen == 8)
_setCommand(CMD_SETSPRITEPATTERN, patternId, patternAddr, patternLen, 0, 0, 0, 0)
return
if (patternLen == 32)
if (patternId & $3)
_logError(String("Invalid patternId in SetSpritePattern(). For 32 byte sprites, must be divisible by 4."))
else
_setCommand(CMD_SETSPRITEPATTERN, patternId, patternAddr, patternLen, 0, 0, 0, 0)
return
_logError(String("Invalid patternLen in SetSpritePattern()."))
PUB SetSprite(spriteId, mask, patternId, xpos, ypos, color)
if ((spriteId > 31) or (mask == 0))
_logError(String("Invalid SetSprite() params."))
else
_setCommand(CMD_SETSPRITE, spriteId, mask, patternId, xpos, ypos, color, 0)
PUB GenerateSpritePatternFromChar(pDest, char, size) |offset, mask, i
if (char > 127)
offset := (char - 128) << 3
mask := $00
else
offset := char << 3
mask := $FF
if (size == 8)
repeat i from 0 to 7
byte[pDest][i] := mask
byte[pDest][i] ^= byte[@CharGen8X8 + offset][i]
return
if (size == 32)
repeat i from 0 to 31
byte[pDest][i] := mask
case i
4..11:
byte[pDest][i] ^= ((byte[@CharGen8X8 + offset][i - 4] >> 4) & $0F)
20..27:
byte[pDest][i] ^= ((byte[@CharGen8X8 + offset][i - 20] << 4) & $F0)
return
_logError(String("Invalid params in GenerateSpritePatternFromChar()"))
{{ Read - only properties}}
PUB NameTable
return (reg[2] & $0F) * $400
PUB ColorTable
if (displayMode == GRAPHICS2)
if (reg[3] & %1000_0000) 'This is simplification as real TMS9918 only recognizes $FF and $7F
return $2000
else
return $0000
else
return reg[3] * $40
PUB PatternTable
if (displayMode == GRAPHICS2)
if (reg[4] & %0000_1000) 'This is a simplification as real TMS9918 only recognizes $07 and $03
return $2000
else
return $0000
return (reg[4] & $07) * $800
PUB SpriteAttributeTable
'_logTrace(String("reg[5]= "), reg[5], String(" SpriteAttributeTable= "), (reg[5] & $7F) * $80, 0, 0, 0)
return (reg[5] & $7F) * $80
PUB SpritePatternTable
'_logTrace(String("reg[6]= "), reg[6], String(" SpritePatternTable= "), (reg[6] & $07) * $800, 0, 0, 0)
return (reg[6] & $07) * $800
PUB TextRowCount
return lastCharRow + 1
PUB TextColumnCount
return lastCharCol + 1
PUB GraphicsVPixelCount
'_logTrace(String("Mode is "), displayMode, String(" GraphicHPixelCount is "), lastPixX + 1 , String(" *GraphicsVPixelCount is "), lastPixY + 1, 8)
return lastPixY + 1
PUB GraphicsHPixelCount
'_logTrace(String("Mode is "), displayMode, String(" *GraphicHPixelCount is "), lastPixX + 1 , String(" GraphicsVPixelCount is "), lastPixY + 1, 8)
return lastPixX + 1
PUB SpriteVPixelCount
return lastSpriteY + 1
PUB SpriteHPixelCount
return lastSpriteX + 1
PUB GoodContrastColors(index)
return byte[@GoodContrastColorsTable][index & $F]
PUB CurrentDisplayMode
return displayMode
PUB WaitASecond
pst.Str(String("Waiting 1s ..."))
pst.NewLine
waitcnt(80_000_000 + cnt) ' wait 1 seconds
'waitcnt((clkfreq * 1) + cnt)
pst.Str(String("Done"))
'pst.Hex(value, 8)
'pst.CharIn
pst.NewLine
{{ Push parameters to memory shared by cogs }}
PRI _setCommand(cmd, param1, param2, param3, param4, param5, param6, param7)
repeat until not lockset(lockCommandBuffer) 'this will block until VDP cog is done executing previous command
LONG[plCommand][0] := cmd
LONG[plCommand][1] := param1
LONG[plCommand][2] := param2
LONG[plCommand][3] := param3
LONG[plCommand][4] := param4
LONG[plCommand][5] := param5
LONG[plCommand][6] := param6
LONG[plCommand][7] := param7
lockclr(lockCommandBuffer)
{{
Private sprite methods
}}
PRI _updateSpritePositions(startId) |i
repeat i from startId to 31 step 2
if (spriteSpeed[i] and ((cnt - lastSpritePositionUpdateCnt[i]) > (clkfreq / 10)))
lastSpritePositionUpdateCnt[i] := cnt
_setSprite(i, SPRITEMASK_DX | SPRITEMASK_DY, 0, byte[@spriteSpeed + (i << 1)][1], byte[@spriteSpeed + (i << 1)][0], 0)
PRI _setSpriteMode(spriteMode)
_setReg(1, reg[1] & %1111_1100 | spriteMode)
PRI _setSprite(spriteId, mask, patternId, x, y, color) |spriteAttributeAddress
spriteAttributeAddress := SpriteAttributeTable + (spriteId << 2)
_copyFromVdpMem(spriteAttributeAddress, @SpriteBuff, 4)
'_logSprite(String("Sprite before "), spriteAttributeAddress, @SpriteBuff)
if (mask & SPRITEMASK_SETY)
byte[@SpriteBuff][0] := y
else
if (mask & SPRITEMASK_DY)
byte[@SpriteBuff][0] += y
else
if (mask & SPRITEMASK_VY)
byte[@spriteSpeed + (spriteId << 1)][1] := y
if (mask & SPRITEMASK_SETX)
byte[@SpriteBuff][1] := x
else
if (mask & SPRITEMASK_DX)
byte[@SpriteBuff][1] += x
else
if (mask & SPRITEMASK_VX)
byte[@spriteSpeed + (spriteId << 1)][0] := x
if (mask & SPRITEMASK_SETPATTERN)
byte[@SpriteBuff][2] := patternId
if (mask & SPRITEMASK_SETCOLOR)
byte[@SpriteBuff][3] := (byte[@SpriteBuff][3] & $F0) | (color & $0F)
'_logSprite(String("Sprite after "), spriteAttributeAddress, @SpriteBuff)
_copyToVdpMem(spriteAttributeAddress, @SpriteBuff, 4)
{{
Private drawing methods
}}
PRI _drawLine(xs, ys, xe, ye, color) |x, y, dx, dy, stepx, stepy, pixCount
'_logTrace(String("Drawing line in color "), color, String(" from "), xs << 16 | ys , String(" to "), xe << 16 | ye, 8)
pixCount := 0
dx := xe - xs
dy := ye - ys
if (dx)
if (dx < 0)
stepx := -1
else
stepx := 1
if (dy) 'dx != 0
if (dy < 0)
stepy := -1
else
stepy := 1
x := xs
y := ys
if (||dy > ||dx)
repeat while (y - ye)
pixCount += _drawPixel(x, y, color)
y := y + stepx
if (_lineError(x + stepx, xs, y, ys, dx, dy) < _lineError(x, xs, y, ys, dx, dy))
x := x + stepx
else
repeat while (x - xe)
pixCount += _drawPixel(x, y, color)
x := x + stepx
if (_lineError(x, xs, y + stepy, ys, dx, dy) < _lineError(x, xs, y, ys, dx, dy))
y := y + stepy
pixCount += _drawPixel(xe, ye, color) 'because repeats above will bail before reaching last pixel
else
'_logTrace(String("Horizontal line in color "), color, String(" from "), xs << 16 | ys , String(" to "), xe << 16 | ye, 8)
repeat x from xs to xe 'dy == 0, horizontal line
pixCount += _drawPixel(x, ys, color)
else
if (dy) 'dx == 0
'_logTrace(String("Vertical line in color "), color, String(" from "), xs << 16 | ys , String(" to "), xe << 16 | ye, 8)
repeat y from ys to ye 'dy != 0, vertical line
pixCount += _drawPixel(xs, y, color)
else
pixCount += _drawPixel(xs, ys, color) 'dy == 0, single dot
return pixCount
PRI _lineError(x, x0, y, y0, dx, dy)
return ||((y - y0) * dx - (x - x0) * dy)
PRI _drawCircle(xc, yc, radius, color) |x, y, x2, y2, r2, x2m, pixCount
'_logTrace(String("Drawing circle in color "), color, String(" at "), xc << 16 | yc , String(" with radius "), radius, 8)
if (radius < 1)
return 0
pixCount := 0
x := radius
y := 0
r2 := radius * radius
x2 := r2
y2 := 0
repeat while (y =< x)
pixCount += _drawPixel(xc + x, yc + y, color)
pixCount += _drawPixel(xc + x, yc - y, color)
pixCount += _drawPixel(xc - x, yc + y, color)
pixCount += _drawPixel(xc - x, yc - y, color)
pixCount += _drawPixel(xc + y, yc + x, color)
pixCount += _drawPixel(xc + y, yc - x, color)
pixCount += _drawPixel(xc - y, yc + x, color)
pixCount += _drawPixel(xc - y, yc - x, color)
y2 := y2 + y + y + 1
y++
x2m := x2 - x - x + 1
if (_circleError(x2m, y2, r2) < _circleError(x2, y2, r2))
x--
x2 := x2m
PRI _circleError(x2, y2, r2)
return ||(r2 - x2 - y2)
PRI _drawPixel(x, y, color) |pVdp, pixByte, mask, name, pixWord
if ((x < 0) or (x > lastPixX))
return 0
if ((y < 0) or (y > lastPixY))
return 0
case displayMode
GRAPHICS2:
pVdp := PatternTable + ((x >> 3) + (y >> 3) << 5) << 3 + (y & $7)
if (pVdp & $FFFF_C000)
_prompt(String("pVdp="), pVdp)
'Get a byte from video memory at the right pixel address
_vdpWrite(pVdp.byte[0], 1)
_vdpWrite(%0000_0000 | pVdp.byte[1], 1)
pixByte := _vdpRead(0)
'Write back to same place with single bit set or reset
'_logTrace(String("Drawing pixel in color "), color, String(" at "), x << 16 | y , String(" in memory location "), pVdp, 8)
_vdpWrite(pVdp.byte[0], 1)
_vdpWrite(%0100_0000 | pVdp.byte[1], 1)
_vdpWrite(lookupz(color & $01 : pixByte & byte[@AndMask + (x & 7)], pixByte | byte[@OrMask + (x & 7)]), 0)
'if (color & $01)
' _vdpWrite(pixByte | byte[@OrMask + (x & 7)] , 0)
'else
' _vdpWrite(pixByte & byte[@AndMask + (x & 7)], 0)
return 1 'return of 1 means visible pixel was changed, and 0 not changed
MULTICOLOR:
pVdp := PatternTable + ((x >> 1) + (y >> 3) << 5) << 3 + (y & $7)
if (pVdp & $FFFF_C000)
_prompt(String("pVdp="), pVdp)
'Get a byte from video memory at the right pixel address
_vdpWrite(pVdp.byte[0], 1)
_vdpWrite(%0000_0000 | pVdp.byte[1], 1)
pixByte := _vdpRead(0)
'Write back to same place with upper or lower nibble set to color
'_logTrace(String("Drawing pixel in color "), color, String(" at "), x << 16 | y , String(" in memory location "), pVdp, 8)
_vdpWrite(pVdp.byte[0], 1)
_vdpWrite(%0100_0000 | pVdp.byte[1], 1)
_vdpWrite(lookupz(x & $1 : (color << 4) | (pixByte & $0F), (color & $0F) | (pixByte & $F0)), 0)
'if (x & $01)
' _vdpWrite((color & $0F) | (pixByte & $F0), 0)
'else
' _vdpWrite((color << 4) | (pixByte & $0F), 0)
return 1
other:
return 0
PRI _drawText(pbText, columnLeft, rowTop, columnRight, rowBottom)|row, column, char
'_logTrace(pbText, displayMode, String(" from "), columnLeft << 16 | rowTop , String(" to "), columnRight << 16 | rowBottom, 8)
repeat row from rowTop to rowBottom
repeat column from columnLeft to columnRight
char := byte[pbText++]
if (char)
if ((row => 0) and (row =< lastCharRow) and (column => 0) and (column =< lastCharCol))
_writeCharAt(char, row, column)
else
return
PRI _writeText(pbText) |char, colOffset
repeat
char := byte[pbText++]
case char
0: return 'end of string
CS: 'clear screen
_clearTextScreen
HM: 'home cursor
_homeTextScreen
LF: 'Line feed, just move one line down, which may cause scrolling
_moveCursorDown
'nextCharCol := 0 'uncommment if you want LF to act as CR+LF
NL: 'Carriage return, move to first left position in this line
nextCharCol := 0
'_moveCursorDown 'uncomment if you want CR to act as CR+LF
ML: 'move cursor left
_moveCursorLeft
MR: 'move cursor right
_moveCursorRight
MU: 'move cursor up
nextCharRow--
if (nextCharRow < 0)
nextCharRow := 0
_scrollDown
MD:
_moveCursorDown
TB: 'tab (1 tab is 8 chars, so we have 4 or 5 tabs per line)
colOffset := nextCharCol & $7
if (colOffset)
repeat (8 - colOffset)
_moveCursorRight
BS: 'backspace (== move towards left and clear the position)
_moveCursorLeft
_writeCharAt(" ", nextCharRow, nextCharCol)
other:
_writeCharAt(char, nextCharRow, nextCharCol)
_moveCursorRight
PRI _moveCursorUp
nextCharRow--
if (nextCharRow < 0)
nextCharRow := 0
_scrollDown
PRI _moveCursorDown
nextCharRow++
if (nextCharRow > lastCharRow)
nextCharRow := lastCharRow
_scrollUp
PRI _moveCursorLeft
nextCharCol--
if (nextCharCol < 0)
nextCharCol := lastCharCol
_moveCursorUp
PRI _moveCursorRight
nextCharCol++
if (nextCharCol > lastCharCol)
nextCharCol := 0
_moveCursorDown
PRI _writeCharAt(char, row, col) |pVdp
pVdp := NameTable + row * (lastCharCol + 1) + col
_vdpWrite(pVdp.byte[0], 1)
_vdpWrite(%0100_0000 | pVdp.byte[1], 1)
_vdpWrite(char, 0)
PRI _homeTextScreen
nextCharRow := 0
nextCharCol := 0
PRI _clearTextScreen
_fillVdpMem(NameTable, (lastCharRow + 1) * (lastCharCol + 1), " ", 0)
PRI _scrollUp |row, colCount
colCount := lastCharCol + 1
repeat row from 1 to lastCharRow
_copyFromVdpMem(NameTable + colCount * row, @RowBuff, colCount)
_copyToVdpMem(NameTable + colCount * (row - 1), @RowBuff, colCount)
_fillVdpMem(NameTable + colCount * lastCharRow, colCount, " ", 0) 'fill last row with spaces
PRI _scrollDown|row, colCount
colCount := lastCharCol + 1
repeat row from lastCharRow - 1 to 0
_copyFromVdpMem(NameTable + colCount * row, @RowBuff, colCount)
_copyToVdpMem(NameTable + colCount * (row + 1), @RowBuff, colCount)
_fillVdpMem(NameTable, colCount, " ", 0) 'fill first row with spaces
{{
Other private methods
}}
PRI _initialize(charWidth, charHeight, pixelWidth, pixelHeight, spriteWidth, spriteHeight, pRegs, enableInterrupt) |index
wordfill(@spriteSpeed, 0, 32)
lastCharCol := charWidth - 1
lastCharRow := charHeight - 1
lastPixX := pixelWidth - 1
lastPixY := pixelHeight - 1
lastSpriteX := spriteWidth - 1
lastSpriteY := spriteHeight - 1
repeat index from 0 to 7
if (index == 1)
_setReg(index, byte[pRegs][index] & %1011_1111) 'force screen turn off
else
_setReg(index, byte[pRegs][index])
if (enableInterrupt)
_setReg(1, reg[1] | %0010_0000) 'enable interrupt
PRI _initCharTable(pVdpDest, use5x7)| invert, pattern, pMemSrc, mask, i
_vdpWrite(pVdpDest.byte[0], 1)
_vdpWrite(%0100_0000 | pVdpDest.byte[1], 1)
repeat invert from 0 to 1 'non-reverse and reverse
if (use5x7)
pMemSrc := @CharGen5x7
repeat 128
repeat i from 7 to 0 'flip rows from chargen (5 bytes) and fill 3 with 0
if (i > 2)
byte[RowBuff][i] := byte[pMemSrc++]
else
byte[RowBuff][i] := 0
mask := %1000_0000
repeat 8 'rotate 8*8 bytes clockwise
pattern := 0
repeat i from 7 to 0
pattern := pattern << 1
if (byte[RowBuff + i] & mask)
pattern |= $01
_vdpWrite(pattern ^ byte[@XorMask][invert], 0)
mask := mask >> 1
else
pMemSrc := @CharGen8x8 'this chargen is in right row-wise format, just copy
repeat 128 * 8
pattern := byte[pMemSrc++]
_vdpWrite(pattern ^ byte[@XorMask][invert], 0)
PRI _fillVdpMem(pVdp, count, value, increment)
'if increment
' _logTrace(String("Fill VDP mem from "), pVdp, String(" to "), pVdp + count - 1, String(" with incrementing value "), value, 2)
'else
' _logTrace(String("Fill VDP mem from "), pVdp, String(" to "), pVdp + count - 1, String(" with constant value "), value, 2)
'setReg(1, reg[1] & %1011_1111) 'blank screen
_vdpWrite(pVdp.byte[0], 1)
_vdpWrite(%0100_0000 | pVdp.byte[1], 1)
repeat count
_vdpWrite(value.byte[0], 0)
value += increment
'setReg(1, reg[1] | %0100_0000) 'show screen
PRI _copyToVdpMem(pVdp, pMain, count)
'_logTrace(String("Copy "), count, String(" bytes from main memory address "), pMain, String(" to VDP address "), pVdp, 8)
'setReg(1, reg[1] & %1011_1111) 'blank screen
_vdpWrite(pVdp.byte[0], 1)
_vdpWrite(%0100_0000 | pVdp.byte[1], 1)
repeat count
_vdpWrite(byte[pMain++], 0)
'setReg(1, reg[1] | %0100_0000) 'show screen
PRI _copyFromVdpMem(pVdp, pMain, count) |val
'_logTrace(String("Copy "), count, String(" bytes from VDP address "), pVdp, String(" to main memory address "), pMain, 8)
'setReg(1, reg[1] & %1011_1111) 'blank screen
_vdpWrite(pVdp.byte[0], 1)
_vdpWrite(%0000_0000 | pVdp.byte[1], 1)
repeat count
byte[pMain++] := _vdpRead(0)
'setReg(1, reg[1] | %0100_0000) 'show screen
PRI _setReg(index, value)
if (index > 7)
_logError(String("Invalid register number."))
else
reg[index] := value
_vdpWrite(value, 1)
_vdpWrite(%1000_0000 | index, 1)
PRI _interval(cntEnd, cntStart)
result := cntEnd - cntStart
if (result < 0)
result := $7FFF_FFFF + result
return result
{{ interfacing with VDP chip }}
PRI _readStatus
return _vdpRead(1)
PRI _vdpRead(modeVal)
if (modeVal == 0) 'only wait if reading from vdp memory, not status reg
_waitForScan
outa[MODE] := modeVal 'set mode
outa[nCSW]~~ 'write inactive
outa[nCSR]~~ 'read inactive
dira[CD0 .. CD7]~ 'data bus is input
outa[nCSR]~ 'pulse nCSR
'outa[nCSR]~ 'delay
'outa[nCSR]~ 'delay
'outa[nCSR]~ 'delay
'outa[nCSR]~ 'delay
'outa[nCSR]~ 'delay
'outa[nCSR]~ 'delay
result := ina[CD0 .. CD7]
outa[nCSR]~~
PRI _vdpWrite(byteVal, modeVal)
if (modeVal == 0) 'only wait if writing to vdp memory, not register
_waitForScan
outa[MODE] := modeVal 'set mode
outa[nCSW]~~ 'write inactive
outa[nCSR]~~ 'read inactive
dira[CD0 .. CD7]~~ 'data bus is output
outa[CD0 .. CD7] := byteVal
'outa[nCSW]~ 'pulse nCSW
'outa[nCSW]~ 'delay
outa[nCSW]~ 'delay
outa[nCSW]~~
PRI _vdpReset
outa[nReset]~
waitcnt((clkfreq / 2) + cnt) '500ms
outa[nReset]~~
PRI _waitForScan
if ((reg[1] & %0110_0000) == %0110_0000) 'only wait if not blanking and in interrupt mode
if ((cnt - lastScanCnt) > vdpAccessWindow)
dira[nInt] := 0 'make sure nInt is input (redundant?)
repeat
if (ina[nInt])
waitpeq(0, |< nInt, 0) 'wait for nInt to go low
lastStatus := _readStatus
until (lastStatus & %1000_0000) 'F bit is set, meaning interrupt due to scan line
lastScanCnt := cnt
{{ various helpers }}
PRI _logCommand(commandString, ellapsedCycles) |stackSize
if (skipTrace)
return
pst.Str(String("TRACE: "))
pst.Str(commandString)
pst.Hex(displayMode, 2)
pst.Str(String(" executed in "))
pst.Dec(ellapsedCycles / (clkfreq / 1_000_000))
pst.Str(String(" us."))
stackSize := STACK_LEN
repeat
stackSize--
while ((stackSize > 0) and (stack[stackSize] == 0))
pst.Str(String(" Stack watermark is "))
pst.Dec(stackSize)
pst.Newline
PRI _logSprite(string1, attrAddr, buffAddr)
if (skipTrace)
return
pst.Str(String("TRACE: "))
pst.Str(string1)
pst.Hex(attrAddr, 8)
pst.Str(String(" y= "))
pst.Hex(byte[buffAddr][0], 2)
pst.Str(String(" x= "))
pst.Hex(byte[buffAddr][1], 2)
pst.Str(String(" name= "))
pst.Hex(byte[buffAddr][2], 2)
pst.Str(String(" color= "))
pst.Hex(byte[buffAddr][3], 2)
pst.Newline
PUB Trace(string1, val1)
_logTrace(string1, val1, 0, 0, 0, 0, 0)
PRI _logTrace(string1, val1, string2, val2, string3, val3, hexChars)
if (skipTrace)
return
pst.Str(String("TRACE: "))
pst.Str(string1)
pst.Hex(val1, 8)
if (string2)
pst.Str(string2)
pst.Hex(val2, 8)
if (string3)
pst.Str(string3)
if (hexChars > 0)
pst.Hex(val3, hexChars)