-
Notifications
You must be signed in to change notification settings - Fork 6
/
PSET VRAM.asm
655 lines (608 loc) · 11.2 KB
/
PSET VRAM.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
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
10 ORG 100H
20 JP MAIN
30RPTCHR EQU 0BFEEH
40GPF EQU 0BFD0H
50AOUT EQU 0BD09H
60PUTSTR EQU 0BFF1H
70WAITK EQU 0BFCDH
80LDPSTR EQU 0BD00H
90REGOUT EQU 0BD03H
100MAIN: CALL CLS
110 LD HL,DPSET
120 CALL STRLN
130 LD DE,0
140 CALL PUTSTR
150 CALL GTVRAM ; get copy of vram
160 LD A,12
170 LD (POSY),A
180 LD A,133
190 LD (POSX),A
200 CALL PSET
210 LD A,120
220 LD (POSX),A
230 LD A,22
240 LD (POSY),A
250 CALL PSET
260 CALL DSPLAY ; copy to screen
270 CALL WAIT
280 LD HL,DHLINE
290 CALL STRLN
300 LD DE,0
310 CALL PUTSTR
320 CALL GTVRAM
330 LD A,32
340 LD (POSY),A
350MAIN00: LD A,10
360 LD (POSX),A
370 LD A,133
380 LD (ENDX),A
390 CALL HLINE ; 10,[32-42] to 133,[32-42]
400 LD A,(POSY)
410 INC A
420 INC A ; +2 increment
430 LD (POSY),A
440 CP 42
450 JP NZ,MAIN00
460 CALL DSPLAY
470 CALL WAIT
480 LD HL,DVLINE
490 CALL STRLN
500 LD DE,0100H
510 CALL PUTSTR
520 CALL GTVRAM
530 LD A,80
540 LD (POSX),A
550 LD A,38
560 LD (ENDY),A
570MAIN02: LD A,0
580 LD (POSY),A
590 CALL VLINE ; [80-90],0 to [80-90],38
600 LD A,(POSX)
610 INC A
620 INC A ; +2 increment
630 LD (POSX),A
640 CP 90
650 JP NZ,MAIN02
660 CALL DSPLAY
670 CALL WAIT
680 CALL CLS
690 CALL GTVRAM
700 LD HL,DPLINE
710 CALL STRLN
720 LD DE,0100H
730 CALL PUTSTR
740 LD A,88
750 LD (ENDX),A
760 LD A,38
770 LD (ENDY),A
780 ;pline.c:19: plotLine(50,2,88,38);
790MAIN04: LD H,0
800 LD A,(ENDY)
810 LD L,A
820 PUSH HL
830 LD A,(ENDX)
840 LD L,A
850 PUSH HL
860 LD A,2
870 LD L,A
880 PUSH HL
890 LD A,50
900 LD L,A
910 PUSH HL
920 ; STACK:
930 ; X0 0-1
940 ; Y0 2-3
950 ; X1 4-5
960 ; Y1 6-7
970 CALL PLINE
980 LD HL,8 ; X0, Y0, X1, Y1
990 ADD HL,SP
1000 LD SP,HL ; POP 8 BYTES AT ONCE
1010 LD A,(ENDY)
1020 CP 6
1030 JP Z,MAIN03
1040 DEC A
1050 LD (ENDY),A
1060 LD A,(ENDX)
1070 INC A
1080 INC A
1090 LD (ENDX),A
1100 JP MAIN04
1110MAIN03: CALL DSPLAY
1120 CALL WAIT
1130 CALL CLS
1140 CALL CLVRAM ; clear screen and vram
1150 LD HL,RNDPNT
1160 CALL STRLN
1170 LD DE,0
1180 CALL PUTSTR
1190 CALL GTVRAM
1200 LD B,8 ; 8 TIMES
1210FILL01: PUSH BC
1220 LD B,0 ; 256 TIMES = 2,048 points
1230FILL00: PUSH BC
1240 CALL PRNG ; pseudo-random number
1250 LD D,143
1260 CALL DVDX ; A = A modulo 143
1270 LD (POSX),A
1280 CALL PRNG
1290 LD D,45
1300 CALL DVDX ; A = A modulo 45
1310 LD (POSY),A
1320 CALL PSET
1330 POP BC
1340 DJNZ FILL00
1350 POP BC
1360 DJNZ FILL01
1370 LD A,48
1380 LD (POSY),A
1390MAIN01: LD A,100
1400 LD (POSX),A
1410 LD A,144
1420 LD (ENDX),A
1430 CALL HLINE ; 100,[48-60] to 144,[48-60]
1440 LD A,(POSY)
1450 INC A
1460 INC A ; +2 increment
1470 LD (POSY),A
1480 CP 60
1490 JP NZ,MAIN01
1500 CALL DSPLAY ; copy to screen
1510 CALL WAIT
1520 LD B,16
1530 LD A,50H
1540 OUT (40H),A
1550 CALL VBWEHT
1560 CALL VBWEHT
1570 CALL VBWEHT
1580 CALL VBWEHT ; 1 SEC
1590 CALL VBWEHT
1600 CALL VBWEHT
1610 CALL VBWEHT
1620 CALL VBWEHT ; 1 SEC
1630 LD A,40H
1640 OUT(40H),A
1650 CALL CLS
1660 LD HL,BYEBYE
1670 CALL STRLN
1680 LD DE,0
1690 CALL PUTSTR
1700 RET
1900WAIT: CALL WAITK
1910 OR A
1920 JP Z,WAIT
1930 RET
2000CLS: LD B,144
2010 LD DE,0000H
2020CLS0: LD A,32
2030 CALL RPTCHR
2040 RET
2050CLLN: LD B,24
2060 LD E,0
2070 JP CLS0
2100CLVRAM: LD HL,VRAM0
2110 AND A
2120 LD B,144
2130CVRAM0: LD (HL),A
2140 INC HL ; clear the first 144 bytes
2150 DJNZ CVRAM0
2160 LD DE,VRAM1
2170 LD BC,144
2180 LD HL,VRAM0
2190 LDIR ; copy that to the next 144 bytes
2200 LD BC,288
2210 LD HL,VRAM0
2220 LDIR ; use the top 288 bytes
2230 LD BC,288
2240 LD HL,VRAM0
2250 LDIR ; to clear the rest
2260 LD BC,288
2270 LD HL,VRAM0
2280 LDIR ; EXTRA 2 LINES FOR SCROLL
2290 RET ; Hat Tip @hwreverse
2300VRAM0: DS 144
2310VRAM1: DS 144
2320VRAM2: DS 144
2330VRAM3: DS 144
2340VRAM4: DS 144
2350VRAM5: DS 144
2360VRAM6: DS 144
2370VRAM7: DS 144
2380GTVRAM: LD HL,VRAM0
2390 LD DE,0000H
2400 LD B,144
2410 CALL LDPSTR ; get 144 chars from Y=0,X=0
2420 LD HL,VRAM1
2430 LD DE,0100H
2440 LD B,144
2450 CALL LDPSTR
2460 LD HL,VRAM2
2470 LD DE,0200H
2480 LD B,144
2490 CALL LDPSTR
2500 LD HL,VRAM3
2510 LD DE,0300H
2520 LD B,144
2530 CALL LDPSTR
2540 LD HL,VRAM4
2550 LD DE,0400H
2560 LD B,144
2570 CALL LDPSTR
2580 LD HL,VRAM5
2590 LD DE,0500H
2600 LD B,144
2610 CALL LDPSTR ; get 144 chars from Y=5,X=0
2620 LD HL,VRAM6
2630 LD DE,0600H
2640 LD B,144
2650 CALL LDPSTR
2660 LD HL,VRAM7
2670 LD DE,0700H
2680 LD B,144
2690 CALL LDPSTR ; EXTRA 2 LINES FOR SCROLL
2700 RET
2710DSPLAY: LD HL,VRAM0
2720 LD DE,0000H
2730 LD B,144
2740 CALL GPF ; display 144 8-bit columns from Y=0,X=0
2750 LD HL,VRAM1
2760 LD DE,0100H
2770 LD B,144
2780 CALL GPF
2790 LD HL,VRAM2
2800 LD DE,0200H
2810 LD B,144
2820 CALL GPF
2830 LD HL,VRAM3
2840 LD DE,0300H
2850 LD B,144
2860 CALL GPF
2870 LD HL,VRAM4
2880 LD DE,0400H
2890 LD B,144
2900 CALL GPF
2910 LD HL,VRAM5
2920 LD DE,0500H
2930 LD B,144
2940 CALL GPF ; display 144 8-bit columns from Y=5,X=0
2950 LD HL,VRAM6
2960 LD DE,0600H
2970 LD B,144
2980 CALL GPF
2990 LD HL,VRAM7
3000 LD DE,0700H
3010 LD B,144
3020 CALL GPF ; EXTRA 2 LINES FOR SCROLL
3030 RET
3500BYTE: PUSH AF
3510 AND 0F0H
3520 RRCA
3530 RRCA
3540 RRCA
3550 RRCA
3560 CALL NIBBLE
3570 INC HL
3580 POP AF
3590 AND 15
3600 CALL NIBBLE
3610 INC HL
3620 RET
3630NIBBLE: SUB 10
3640 JP M,ZERO9
3650 ADD A,7
3660ZERO9: ADD A,58
3670 LD (HL),A
3680 RET
4000CALCXY: LD HL,VRAM0
4010 LD A,(POSY)
4020 SRL A
4030 SRL A
4040 SRL A ; Y / 8
4050 OR A ; cp 0
4060 JP Z,CALC00 ; already where we want to be
4070 LD B,A
4080 LD DE,144 ; add a line's worth
4090CALC01: ADD HL,DE
4100 DJNZ CALC01 ; loop
4110CALC00: ; HL = beginning of line Y
4120 LD A,(POSX) ; x
4130 LD C,A
4140 LD B,0
4150 ADD HL,BC ; HL = exactly at pos x,y
4160 LD A,(POSY) ; y
4170 CALL DVD8 ; A = bit offset
4180 LD (REMNY),A
4190 OR A ; cp 0
4200 JP NZ,CALC02 ; if A = 0,set to 1
4210 LD A,1
4220 JP CALC03
4230CALC02: LD B,A ; else shift '1' left B times
4240 LD A,1
4250CALC04: SLA A; A << 1
4260 DJNZ CALC04
4270CALC03: LD B,A ; bit offset in B
4280 LD A,(HL) ; 8 bits in A
4290 RET ; Now do what you want with A (current bit pattern) and B (bit offset)
4380PSET: CALL CALCXY
4390 OR B ; set to black
4400 LD (HL),A
4440 RET
4450PUNSET: CALL CALCXY
4460 XOR B ; set to white
4470 LD (HL),A
4510 RET
4520PGET: CALL CALCXY
4530 AND B ; is it black?
4540 RET
4550HLINE: CALL CALCXY ; don't call PSET (and CALCXY) repeatedly
4560HLINE0: OR B ; DIY. set to black
4570 LD (HL),A
4580 LD A,(ENDX)
4590 LD D,A
4600 LD A,(POSX)
4610 INC A
4620 LD (POSX),A
4630 CP D ; are we done?
4640 RET Z ; yes,bail.
4650 INC HL ; no,increment HL
4660 LD A,(HL)
4670 JP HLINE0 ; and loop
4680 RET
4690VLINE: CALL CALCXY ; harder to optimize as HLINE
4700VLINE0: OR B ; DIY. set to black
4710 LD (HL),A
4720 LD A,(ENDY)
4730 LD D,A
4740 LD A,(POSY)
4750 INC A
4760 LD (POSY),A
4770 CP D ; are we done?
4780 RET Z ; yes,bail.
4790 LD A,B ; No. Do we have to skip one line?
4800 CP 128 ; Last bit in the column?
4810 JP NZ,VLINE1 ; Nah,just increase bit offset
4820 LD DE,144 ; yeah,go one line (8 rows) down
4830 ADD HL,DE
4840 LD B,1 ; reset bit offset
4845 LD A,(HL) ; get the next value
4850 JP VLINE0
4860VLINE1: SLA B ; increase bit offset
4865 LD A,(HL) ; get the next value
4870 JP VLINE0
4880POSX: DB 0
4890POSY: DB 0
4900ENDX: DB 0
4910ENDY: DB 0
4920REMNX: DB 0
4930CHARX: DB 0
4940REMNY: DB 0
4950CHARY: DB 0
4960BUFFER: DB 0,0,0,0,0,0
4970TMP: DB 0
4980HX2DEC: PUSH HL
4990 LD B,'0'
5000 LD (HL),B
5010HDEC00: LD (TMP),A
5020 CP 100
5030 JP M,HDEC01
5040 INC (HL)
5050 SUB 100
5060 JP HDEC00
5070HDEC01: INC HL
5080 LD B,'0'
5090 LD (HL),B
5100 LD A,(TMP)
5110HDEC02: LD (TMP),A
5120 CP 10
5130 JP M,HDEC03
5140 INC (HL)
5150 SUB 10
5160 JP HDEC02
5170HDEC03: INC HL
5180 LD A,(TMP)
5190 ADD A,48
5200 LD (HL),A
5210 POP HL ; remove heading 0s
5220 LD A,(HL)
5230 CP '0'
5240 JP NZ,HDEC05
5250 LD A,' '
5260 LD (HL),A
5270HDEC04: INC HL
5280 LD A,(HL)
5290 CP '0'
5300 JP NZ,HDEC05
5310 LD A,' '
5320 LD (HL),A
5330HDEC05: RET
6000 ; A <-- INPUT
6010 ; A --> REMAINDER. B --> QUOTIENT
6020DVD6: LD B,0
6030DVD6A: CP 6
6040 RET M
6050 SUB 6
6060 INC B
6070 JP DVD6A
6080DVD8: LD B,0
6090DVD8A: CP 8
6100 RET M
6110 SUB 8
6120 INC B
6130 JP DVD8A
6140DVDX: LD B,0
6150DVDXA: CP D
6160 RET M
6170 SUB D
6180 INC B
6190 JP DVDXA
6200STRLN: LD B,0
6210 PUSH HL ;preserve HL
6220STRLN0: LD A,(HL)
6230 OR A
6240 JP Z,STRLN1
6250 INC HL
6260 INC B
6270 JP STRLN0
6280STRLN1: POP HL ;restore HL
6290 RET
6300DPSET: DB 'PSET ',0
6310DHLINE: DB 'HLINE ',0
6320DVLINE: DB 'VLINE ',0
6330DPLINE: DB 'PLINE ',0
6340RNDPNT: DB 'Random points',0
6350BYEBYE: DB 'Goodbye!',0
7000 ; change Seed value for LCM PRNG
7010PRNG: PUSH DE ; Code by @hwreverse
7020 PUSH HL
7030 LD DE,(RVAL16)
7040 LD H,E
7050 LD L,1
7060 ADD HL,DE
7070 ADD HL,DE
7080 ADD HL,DE
7090 ADD HL,DE
7100 ADD HL,DE
7110 LD A,H
7120 LD (RVAL16),HL
7130 POP HL
7140 POP DE
7150 LD A,(RVAL88) ; 0-255
7170 RET ; will use modulo to restrict range
7200RVAL16: DB 30H
7210RVAL88: DB 81H
7300DX: DB 0
7310DY: DB 0
7320MYD: DB 0
7330YI: DB 0
7500VBWEHT: LD B,250
7510BWEHT: PUSH BC
7520 CALL WEHT ; ~1 millisec
7530 POP BC
7540 DJNZ BWEHT
7550 RET
7560WEHT: LD B,241
7570WEHT0: NOP
7580 NOP
7590 NOP
7600 NOP
7610 NOP
7620 DJNZ WEHT0
7630 RET
8000PLINE: PUSH IX
8010 LD IX,0
8020 ADD IX,SP
8030 LD HL,-6
8040 ADD HL,SP
8050 LD SP,HL
8060 ; STACK:
8070 ; IX 0-1
8080 ; RET 2-3
8090 ; X0 4-5
8100 ; Y0 6-7
8110 ; X1 8-9
8120 ; Y1 10-11
8130 ;PLINE.C:4: INT DX = X1 - X0;
8140 LD A,(IX+8) ; X1 LSB
8150 SUB (IX+4) ; X0 LSB
8160 LD L,A
8170 LD A,(IX+9) ; X1 MSB
8180 SBC A,(IX+5) ; X0 MSB
8190 LD H,A ; HL = DX
8200 ;PLINE.C:5: INT DY = Y1 - Y0;
8210 LD A,(IX+10) ; Y1 LSB
8220 SUB (IX+6) ; Y0 LSB
8230 LD E,A
8240 LD A,(IX+11) ; Y1 MSB
8250 SBC A,(IX+7) ; Y0 MSB
8260 LD D,A ; DE = DY
8270 ;PLINE.C:6: INT D = 2*DY - DX;
8280 EX DE,HL
8290 ADD HL,HL
8300 EX DE,HL ; DE = 2*DY
8310 INC SP
8320 INC SP
8330 PUSH DE
8340 LD A,(IX-6) ; DE LSB
8350 SUB L ; MINUS DX LSB
8360 LD C,A
8370 LD A,(IX-5) ; DE MSB
8380 SBC A,H ; MINUS DX MSB
8390 LD B,A ; BC = 2*DY - DX
8400 ;PLINE.C:7: INT Y = Y0;
8410 LD E,(IX+6) ; Y0 LSB
8420 LD D,(IX+7) ; Y0 MSB --> DE = Y
8430 ;PLINE.C:8: FOR (INT X = X0; X <= X1; X++) {
8440 LD A,(IX+4) ; X0 LSB
8450 LD(IX-2),A
8460 LD A,(IX+5) ; X0 MSB
8470 LD(IX-1),A
8480 ADD HL,HL
8490 LD(IX-4),L
8500 LD(IX-3),H ; SAVE 2*SX?
8510L00105: LD A,(IX+8) ; X1 LSB
8520 SUB (IX-2) ; X0 LSB
8530 LD A,(IX+9) ; X1 MSB
8540 SBC A,(IX-1) ; X0 MSB
8550 JP PO,L00125
8560 XOR 80H
8570L00125: JP M,L00107 ; END OF LOOP
8580 ;PLINE.C:9: PLOT(X,Y);
8590 PUSH BC
8600 PUSH DE
8610 ; PUSH DE ; DE = Y
8620 LD L,(IX-2) ; X0 LSB
8630 LD H,(IX-1) ; X0 MSB
8640 ; PUSH HL
8650 ; DON'T PUSH ANYTHING – JUST USE L AND E
8660 LD A,L
8670 CP 143
8680 JP P,PLINE0
8690 LD (POSX),A
8700 LD A,E
8710 CP 47
8720 JP P,PLINE0
8730 LD (POSY),A
8740 CALL PSET
8750PLINE0: POP DE
8760 POP BC
8770 ;PLINE.C:10: IF (D > 0) {
8780 XOR A
8790 CP C
8800 SBC A,B
8810 JP PO,L00126
8820 XOR 80H
8830L00126: JP P,L00102
8840 ;PLINE.C:11: Y = Y + 1;
8850 INC DE
8860 ;PLINE.C:12: D = D - 2*DX;
8870 LD A,C
8880 SUB (IX-4)
8890 LD C,A
8900 LD A,B
8910 SBC A,(IX-3)
8920 LD B,A
8930L00102:
8940 ;PLINE.C:14: D = D + 2*DY;
8950 LD A,C
8960 ADD A,(IX-6)
8970 LD C,A
8980 LD A,B
8990 ADC A,(IX-5)
9000 LD B,A
9010 ;PLINE.C:8: FOR (INT X = X0; X <= X1; X++) {
9020 INC(IX-2)
9030 JR NZ,L00105
9040 INC(IX-1)
9050 JR L00105
9060L00107:
9070 ;PLINE.C:16: }
9080 LD SP,IX
9090 POP IX
9100 ; STACK:
9110 ; RET 0-1
9120 ; X0 2-3
9130 ; Y0 4-5
9140 ; X1 6-7
9150 ; Y1 8-9
9160 RET