-
Notifications
You must be signed in to change notification settings - Fork 3
/
petrock.asm
1832 lines (1505 loc) · 65.1 KB
/
petrock.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
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
;-----------------------------------------------------------------------------------
; PETROCK: Spectrum Analyzer Display for C64 and PET
;-----------------------------------------------------------------------------------
; (c) Plummer's Software Ltd, 02/11/2022 Initial commit
; David Plummer
; Rutger van Bergen
;-----------------------------------------------------------------------------------
;
; General Idea for Newcomers:
;
; Draws 16 vertical bands of the spectrum analyzer which can be up to 16 high. The
; program first clears the screen, draws the border and text, fills in color, and the
; main draw loop calls DrawBand for each one in turn. Each frame draws a new set of
; peaks from the PeakData table, which has 16 entries, one per band. That data is
; replaced either by a new frame of demo data or an incoming serial packet and the
; process is repeated, running at about 40 fps.
;
; Color RAM can be filled with different patterns by stepping through the visual styles
; with the C key, but it is not drawn each and every frame.
;
; Basic bar draw is to walk down the bar and draw a blank (when above the bar), the top
; of the bar, then the middle pieces, then the bottom. A visual style definition is
; set that includes all of the PETSCII chars you need to draw a band, like the corners
; and sides, etc. It can be changed with the S key.
;
; Every frame the serial port is checked for incoming data which is then stored in the
; SerialBuf. If that fills up without a nul it is reset, but if a nul comess in at the
; right place (right packet size) and the magic byte matches, it is used as new peakdata
; and stored in the PeakData table. The code on the ESP32 sends it over as 16 nibbles
; packed into 8 bytes plus a VU value.
;
; The built-in serial code on the C64 is poor, and serial/c64/driver.s contains a new
; impl that works well for receiving data up to 4800 baud.
; On the PET, built-in serial code is effectively absent. For the PET,
; serial/c64/driver.s contains an implementation that is confirmed to receive data
; up to 2400 baud.
;
;-----------------------------------------------------------------------------------
.SETCPU "6502"
; Include the system headers and application defintions ----------------------------
.include "settings.inc"
.include "petrock.inc" ; Project includes and defintions
; Our BSS Data --------------------------------------------------------------------
.org SCRATCH_START ; Program counter to casssette buffer so that
.bss ; we can define our BSS storage variables
; These are local BSS variables. We're using the cassette buffer for storage. All
; will be initlialzed to 0 bytes at application startup.
ScratchStart:
tempDrawLine: .res 1 ; Temp used by DrawLine
tempOutput: .res 1 ; Temp used by OutputSymbol
tempX: .res 1 ; Preserve X Pos
tempY: .res 1 ; Preserve Y Pos
lineChar: .res 1 ; Line draw char
SquareX: .res 1 ; Args for DrawSquare
SquareY: .res 1
Width: .res 1
Height: .res 1 ; Height of area to draw
ClearHeight: .res 1 ; Height of area to clear
DataIndex: .res 1 ; Index into fakedata for demo
resultLo: .res 1 ; Results from multiply operations
resultHi: .res 1
VU: .res 1 ; VU Audio Data
Peaks: .res NUM_BANDS ; Peak Data for current frame
NextStyle: .res 1 ; The next style we will pick
CharDefs: .res VISUALDEF_SIZE ; Storage for the visualDef currently in use
RedrawFlag: .res 1 ; Flag to redraw screen
DemoMode: .res 1 ; Demo mode enabled
.if C64 ; Color's only relevant on the C64
CurSchemeIndex: .res 1 ; Current band color scheme index
BorderColor: .res 1 ; Border color at startup
BkgndColor: .res 1 ; Background color at startup
TextColor: .res 1 ; Text color at startup
.endif
TextTimeout: .res 1 ; Text timeout second count (0 = disabled)
.if PET ; Rudimentary approach for PET. The C64 uses a CIA timer
TextCountDown: .res 2 ; Text timeout countdown timer
.endif
.if .not (PET && SERIAL)
DemoToggle: .res 1 ; Update toggle to delay demo mode updates
.endif
.if SERIAL ; Include serial driver variables
SerialBufPos: .res 1 ; Current index into serial buffer
SerialBuf: .res PACKET_LENGTH ; Serial buffer for: "DP" + 1 byte vu + 8 PeakBytes
SerialBufLen = *-SerialBuf ; Length of Serial Buffer
.if C64
.include "serial/c64/vars.s"
.elseif PET
.include "serial/pet/vars.s"
.endif
.endif
ScratchEnd:
.assert * <= SCRATCH_END, error ; Make sure we haven't run off the end of the buffer
.if SERIAL
.assert SerialBufLen = PACKET_LENGTH, error
.endif
; Start of Binary -------------------------------------------------------------------
.code
; BASIC program to load and execute ourselves. Lines of tokenized BASIC that
; have a banner comment and then a SYS command to start the machine language code.
.org 0000 ; File begins with program start address so we
.word BASE ; emit that as the first two bytes
.org BASE
Line10: .word Line1 ; Next line number
.word 0 ; Line Number 10
.byte TK_REM ; REM token
.literal " - SPECTRUM ANALYZER DISPLAY", 00
Line1: .word Line2
.word 1
.byte TK_REM
.literal " - C64PETROCK.COM", 00
Line2: .word Line3
.word 2
.byte TK_REM
.literal " - PETROCK - COPYRIGHT 2022", 00
Line3: .word endOfBasic ; PTR to next line, which is 0000
.word 3 ; Line Number 20
.byte TK_SYS ; SYS token
.literal .sprintf(" %d", PROGRAM)
.byte 00
endOfBasic: .word 00
.res PROGRAM - *
;-----------------------------------------------------------------------------------
; Start of Assembly Code
;-----------------------------------------------------------------------------------
.if PET
lda PET_DETECT ; Check if we're dealing with original ROMs
cmp #PET_2000
bne @goodpet
ldy #>notonoldrom ; Disappoint user
lda #<notonoldrom
jsr WriteLine
rts
@goodpet:
.endif
.if SERIAL
jmp start
.if C64
.include "serial/c64/driver.s"
.elseif PET
.include "serial/pet/driver.s"
.endif
.endif
start:
cld ; Turn off decimal mode
jsr InitVariables ; Zero (init) all of our BSS storage variables
.if C64 ; TOD and color only available on C64
jsr InitTODClocks
lda VIC_BORDERCOLOR ; Save current colors for later
sta BorderColor
lda VIC_BG_COLOR0
sta BkgndColor
lda TEXT_COLOR
sta TextColor
lda #BLACK ; Screen and border to black
sta VIC_BG_COLOR0
sta VIC_BORDERCOLOR
.endif
ldy #>clrGREEN ; Set cursor to green and clear screen, setting text
lda #<clrGREEN ; color to light green on the C64
jsr WriteLine
jsr EmptyBorder ; Draw the screen frame and decorations
jsr SetNextStyle ; Select the first visual style
.if C64 ; Color only supported on C64
jsr FillBandColors ; Do initial fill of band color RAM
.endif
.if SERIAL
jsr OpenSerial ; Open the serial port for data from the ESP32
jsr StartSerial ; Enable Serial! Behold the power!
.endif
drawLoop:
.if SERIAL
jsr GetSerialChar
cmp #$ff ; If byte is $ff, check if "no data" was flagged
bne @havebyte
cpx #<SER_ERR_NO_DATA
bne @havebyte
cpy #>SER_ERR_NO_DATA
beq @donedata
@havebyte: jsr GotSerial
jmp drawLoop
.endif
.if TIMING && C64 ; If 'TIMING' is defined on the C64 we turn the border bit RASTHI
jsr InitTimer ; Prep the timer for this frame
lda #$11 ; Start the timer
sta CIA2_CRA
@waitforraster: bit RASTHI
bmi @waitforraster
lda #DARK_GREY ; Color to different colors at particular
sta VIC_BORDERCOLOR ; places in the draw code to help see how
.endif
@donedata: lda DemoMode ; Load demo data if demo mode is on
beq @redraw
jsr FillPeaks
ldx #$10
ldy #$ff
@delay: dey
bne @delay
dex
bne @delay
@redraw: lda RedrawFlag
beq @afterdraw ; We didn't get a complete packet yet, so no point in drawing anything
lda #0
sta RedrawFlag ; Acknowledge packet
jsr DrawVU ; Draw the VU bar at the top of the screen
.if TIMING && C64 ; If 'TIMING' is defined we turn the border
lda #LIGHT_GREY ; color to different colors at particular
sta VIC_BORDERCOLOR ; places in the draw code to help see how
.endif ; long various parts of it are taking.
ldx #NUM_BANDS - 1 ; Draw each of the bands in reverse order
:
lda Peaks, x ; X = band numner, A = value
jsr DrawBand
dex
bpl :-
.if PET
jsr DownTextTimer ; On the PET, decrease the text timer to compensate
.endif ; for drawing time
.if SERIAL && (C64 || (PET && SENDSTAR))
lda #'*' ; Send a * back to the host
jsr PutSerialChar
.endif
.if TIMING && C64
; Check to see its time to scroll the color memory
lda #BLACK
sta VIC_BORDERCOLOR
: bit RASTHI
bpl :-
lda #0 ; Stop the clock
sta CIA2_CRA
lda #LIGHT_BLUE
sta TEXT_COLOR
ldx #24 ; Print "Current Frame" banner
ldy #09
clc
jsr PlotEx
ldy #>framestr
lda #<framestr
jsr WriteLine
lda CIA2_TB ; Display the number of ms the frame took. I realized
eor #$FF ; that 65536 - time is the same as flipping the bits,
tax ; so that's why I XOR instead of subtracting
lda CIA2_TB+1
eor #$ff
jsr BASIC_INTOUT
lda #' '
jsr CHROUT
lda #'M'
jsr CHROUT
lda #'S'
jsr CHROUT
lda #' '
jsr CHROUT
jsr CHROUT
.endif ; TIMING && C64
@afterdraw: jsr CheckTextTimer
.if SERIAL
jsr GetKeyboardChar ; Get a character from the serial driver's keyboard handler
.else
jsr GETIN ; No serial, use regular GETIN routine
.endif
cmp #0
bne @notEmpty
jmp drawLoop
@notEmpty:
cmp #KEY_S
bne @notStyle
jsr SetNextStyle
jmp drawLoop
@notStyle:
.if C64 ; Color only available on C64
cmp #KEY_C
bne @notColor
jsr SetNextScheme
jmp drawLoop
@notColor: cmp #KEY_C_SHIFT
bne @notShiftC
jsr SetPrevScheme
jmp drawLoop
@notShiftC:
.endif
cmp #KEY_D
bne @notDemo
jsr SwitchDemoMode
jmp drawLoop
@notDemo: cmp #KEY_B
bne @notborder
jsr ToggleBorder
jmp drawLoop
@notborder: cmp #KEY_RUNSTOP
beq @exit
jsr ShowHelp
jmp drawLoop
@exit:
.if SERIAL
jsr CloseSerial
.endif
.if C64 ; Color only available on C64
lda BorderColor ; Restore colors to how we found them
sta VIC_BORDERCOLOR
lda BkgndColor
sta VIC_BG_COLOR0
lda TextColor
sta TEXT_COLOR
.endif
jsr ClearScreen
ldy #>exitstr ; Output exiting text and exit
lda #<exitstr
jsr WriteLine
rts
;-----------------------------------------------------------------------------------
; ToggleBorder - Toggle border around spectrum analyzer area
;-----------------------------------------------------------------------------------
ToggleBorder: lda #<SCREEN_MEM
sta zptmp
lda #>SCREEN_MEM
sta zptmp+1
ldy #0
lda (zptmp),y
cmp #' '
bne ClrBorderMem
; Note: this routine flows into the next one
;-----------------------------------------------------------------------------------
; EmptyBorder - Draw border around spectrum analyzer area
;-----------------------------------------------------------------------------------
EmptyBorder: lda #0
sta SquareX
sta SquareY
lda #XSIZE
sta Width
lda #YSIZE
sta Height
jsr DrawSquare
.if C64 ; Color only available on C64
jsr InitVU ; Let the VU meter paint its color mem, etc
lda #LIGHT_BLUE
sta TEXT_COLOR
.endif
ldy #XSIZE/2-titlelen/2+1 ; Print title banner
ldx #YSIZE-1
clc
jsr PlotEx
ldy #>titlestr
lda #<titlestr
jsr WriteLine
rts
;-----------------------------------------------------------------------------------
; ClearBorder Remove border and decorations
;-----------------------------------------------------------------------------------
ClearBorder:
lda #<SCREEN_MEM
sta zptmp
lda #>SCREEN_MEM
sta zptmp+1
ClrBorderMem: ldy #XSIZE-1 ; Top line
lda #' '
: sta (zptmp),y
dey
bpl :-
ldx #YSIZE-2
@rowloop: lda zptmp ; Left and right lines
clc
adc #XSIZE
sta zptmp
lda zptmp+1
adc #0
sta zptmp+1
lda #' '
ldy #0
sta (zptmp),y
ldy #XSIZE-1
sta (zptmp),y
dex
bne @rowloop
lda zptmp ; Bottom line
clc
adc #XSIZE
sta zptmp
lda zptmp+1
adc #0
sta zptmp+1
ldy #XSIZE-1
lda #' '
: sta (zptmp),y
dey
bpl :-
rts
.if SERIAL
;-----------------------------------------------------------------------------------
; GotSerial Process incoming serial bytes from the ESP32
;-----------------------------------------------------------------------------------
; Store character in serial buffer. Processes packet if character completes it.
;-----------------------------------------------------------------------------------
GotSerial: ldy SerialBufPos
cpy #SerialBufLen
bne @nooverflow
ldy #0
sty SerialBufPos
rts
@nooverflow:
sta SerialBuf, y
iny
sty SerialBufPos
cmp #00 ; Look for carriage return meaning end
beq :+
rts ; No CR, back to caller
: cpy SerialBufPos ; Are we in the right char pos for it?
beq :+ ; Yep - Process packet
ldy #0 ; Nope - Restart filling buffer
sty SerialBufPos
beq @done
: jsr GotSerialPacket
@done: rts
BogusData:
ldy #0
sty SerialBufPos
rts
;-----------------------------------------------------------------------------------
; GotSerialPacket - Recieved a string followed by a carriage return so inspect it
; to see if it could be a data packet, as indicated by 'DP' as
; the first two bytes. Data Packet? Dave Plummer? You decide!
;-----------------------------------------------------------------------------------
GotSerialPacket:
ldy SerialBufPos ; Get received packet length
lda SerialBuf ; Look for 'D'
cmp #MAGIC_BYTE_0
bne BogusData
lda SerialBuf+MAGIC_LEN
.if COL80
asl
.endif
sta VU
PeakDataNibbles = SerialBuf + MAGIC_LEN + VU_LEN
ldy #0
ldx #0
: lda PeakDataNibbles, y ; Get the next byte from the buffer
and #%11110000 ; Get the top nibble
lsr
lsr
lsr
lsr
clc
adc #1 ; Add one to values
sta Peaks+1, x ; Store it in the peaks table
lda PeakDataNibbles, y ; Get that SAME byte from the buffer
and #%00001111 ; Now we want the low nibble
clc
adc #1
sta Peaks, x ; Store it in the peaks table
inx ; Advance to the next peak
inx
iny ; Advance to the next byte of serial data
cpy #8 ; Have we done bytes 0-3 yet?
bne :- ; Repeat until we have
lda #1
sta RedrawFlag ; Time to redraw!
rts
.endif ; SERIAL
;-----------------------------------------------------------------------------------
; FillPeaks
;-----------------------------------------------------------------------------------
; Copy data from the current index of the fake data table to the current peak data
; and vu value
;-----------------------------------------------------------------------------------
FillPeaks:
.if .not (PET && SERIAL)
lda DemoToggle
eor #$01
sta DemoToggle
beq @proceed
rts
@proceed:
.endif
tya
pha
txa
pha
ldx DataIndex ; Multiply the row number by 16 to get the offset
ldy #16 ; into the data table
jsr Multiply
lda resultLo ; Now add the offset and the table base together
clc ; and store the resultant ptr in zptmpC
adc #<AudioData
sta zptmpB
lda resultHi
adc #>AudioData
sta zptmpB+1
ldy #15 ; Copy the 16 bytes at the ptr address to the
: lda (zptmpB), y ; PeakData table
and #$0f ; Normalize value between 1 and 16
clc
adc #1
sta Peaks, y
dey
bpl :-
lda #<PeakData ; Copy the single VU byte from the PeakData
sta zptmpB ; table into the VU variable
lda #>PeakData
sta zptmpB+1
ldy DataIndex
lda (zptmpB), y
.if COL80
asl
.endif
sta VU
lda #1
sta RedrawFlag ; Time to redraw!
inc DataIndex ; Inc DataIndex - Assumes wrap, so if you
; have exacly 256 bytes, you'd need to
; check and fix that here
pla
tax
pla
tay
rts
;-----------------------------------------------------------------------------------
; InitVariables
;-----------------------------------------------------------------------------------
; We use a bunch of storage in the system (on the C64 it's the datasette buffer) and
; it starts out in an unknown state, so we have code to zero it or set it to defaults
;-----------------------------------------------------------------------------------
InitVariables: ldx #ScratchEnd-ScratchStart
lda #$00 ; Init variables to #0
: sta ScratchStart, x
dex
cpx #$ff
bne :-
lda #1
sta RedrawFlag
rts
;-----------------------------------------------------------------------------------
; SwitchDemoMode
;-----------------------------------------------------------------------------------
; Toggle demo mode. If we switch it off, clear peaks and VU data.
;-----------------------------------------------------------------------------------
SwitchDemoMode: lda DemoMode ; Toggle demo mode bit
eor #$01
sta DemoMode
bne @enabled ; If we enabled demo mode, we're done
lda #0 ; Zero out peaks and VU
ldy #15
: sta Peaks, y
dey
bpl :-
sta VU
lda #1
sta RedrawFlag ; Force redraw
ldx #<DemoOffText ; Tell user what just happened
ldy #>DemoOffText
bne @done
@enabled: ldx #<DemoOnText
ldy #>DemoOnText
@done: jmp ShowTextLine
;-----------------------------------------------------------------------------------
; GetCursorAddr - Returns address of X/Y position on screen
;-----------------------------------------------------------------------------------
; IN X: X pos
; IN Y: Y pos
; OUT X: lsb of address
; OUT Y: msb of address
;-----------------------------------------------------------------------------------
ScreenLineAddresses:
.word SCREEN_MEM + 0 * XSIZE, SCREEN_MEM + 1 * XSIZE
.word SCREEN_MEM + 2 * XSIZE, SCREEN_MEM + 3 * XSIZE
.word SCREEN_MEM + 4 * XSIZE, SCREEN_MEM + 5 * XSIZE
.word SCREEN_MEM + 6 * XSIZE, SCREEN_MEM + 7 * XSIZE
.word SCREEN_MEM + 8 * XSIZE, SCREEN_MEM + 9 * XSIZE
.word SCREEN_MEM + 10 * XSIZE, SCREEN_MEM + 11 * XSIZE
.word SCREEN_MEM + 12 * XSIZE, SCREEN_MEM + 13 * XSIZE
.word SCREEN_MEM + 14 * XSIZE, SCREEN_MEM + 15 * XSIZE
.word SCREEN_MEM + 16 * XSIZE, SCREEN_MEM + 17 * XSIZE
.word SCREEN_MEM + 18 * XSIZE, SCREEN_MEM + 19 * XSIZE
.word SCREEN_MEM + 20 * XSIZE, SCREEN_MEM + 21 * XSIZE
.word SCREEN_MEM + 22 * XSIZE, SCREEN_MEM + 23 * XSIZE
.word SCREEN_MEM + 24 * XSIZE
.assert( (* - ScreenLineAddresses) = YSIZE * 2), error
GetCursorAddr: tya
asl
tay
txa
clc
adc ScreenLineAddresses,y
tax
lda ScreenLineAddresses+1,y
adc #0
tay
rts
;-----------------------------------------------------------------------------------
; ClearScreen Guess.
;-----------------------------------------------------------------------------------
ClearScreen: jmp CLRSCR
;-----------------------------------------------------------------------------------
; WriteLine Writes a line of text to the screen using CHROUT ($FFD2)
;-----------------------------------------------------------------------------------
; Y: MSB of address of null-terminated string
; A: LSB
;-----------------------------------------------------------------------------------
WriteLine: sta zptmp
sty zptmp+1
WLRaw: ldy #0
@loop: lda (zptmp),y
beq @done
jsr CHROUT
iny
bne @loop
@done: rts
;-----------------------------------------------------------------------------------
; ShowHelp Show help text
;-----------------------------------------------------------------------------------
ShowHelp:
lda #0
ldx #<EmptyText
ldy #>EmptyText
jsr PutText
lda #1
ldx #<HelpText1
ldy #>HelpText1
jsr PutText
lda #2
ldx #<HelpText2
ldy #>HelpText2
jsr PutText
lda #3
sta TextTimeout
jmp StartTextTimer
;-----------------------------------------------------------------------------------
; ShowTextLine - Puts a line of text in the middle of the text block
;-----------------------------------------------------------------------------------
; X: LSB of address of null-terminated string
; Y: MSB
;-----------------------------------------------------------------------------------
ShowTextLine:
txa
pha
tya
pha
lda #0
ldx #<EmptyText
ldy #>EmptyText
jsr PutText
lda #2
ldx #<EmptyText
ldy #>EmptyText
jsr PutText
lda #1
sta TextTimeout
jsr StartTextTimer
pla
tay
pla
tax
lda #1
; Note: this routine flows into the next one
;-----------------------------------------------------------------------------------
; PutText Put a string of characters at the center of a message line
;-----------------------------------------------------------------------------------
; A: Message line number within the text block
; X: LSB of address of null-terminated string
; Y: MSB
;-----------------------------------------------------------------------------------
PutText:
stx zptmp
sty zptmp+1
clc ; Set cursor to start of desired line
adc #TOP_MARGIN+BAND_HEIGHT
tax
ldy #LEFT_MARGIN
.if C64 ; Color only available on C64
lda #WHITE
sta TEXT_COLOR
.endif
jsr PlotEx
ldy #$ff ; Determine length of string by counting until NUL
: iny
lda (zptmp),y
bne :-
dey
tya ; Calculate number of spaces to center text
clc
sbc #TEXT_WIDTH ; Subtract screen width from text length and invert
eor #$ff ; negative result to get total whitespaces around
lsr ; text. Divide that by 2.
tax
tay
lda #' ' ; Write leading whitespace
: jsr CHROUT
dey
bne :-
jsr WLRaw ; Write text
txa
tay
lda #' ' ; Write trailing whitespace
: jsr CHROUT
dey
bne :-
rts
;-----------------------------------------------------------------------------------
; DrawVU Draw the current VU meter at the top of the screen
;-----------------------------------------------------------------------------------
.if C64 ; Color only available on the C64
; Color memory bytes that will back the VU meter, and only need to be set once
VUColorTable: .byte RED, RED, RED, YELLOW, YELLOW, YELLOW, YELLOW, YELLOW
.byte GREEN, GREEN, GREEN, GREEN, GREEN, GREEN, GREEN, GREEN, GREEN
.byte BLACK, BLACK
.byte GREEN, GREEN, GREEN, GREEN, GREEN, GREEN, GREEN, GREEN, GREEN
.byte YELLOW, YELLOW, YELLOW, YELLOW, YELLOW, RED, RED, RED
VUColorTableLen = * - VUColorTable
.assert(VUColorTableLen >= MAX_VU * 2 + 2), error ; VU plus two spaces in the middle
; Copy the color memory table for the VU meter to the right place in color RAM
InitVU: ldy #VUColorTableLen-1
: lda VUColorTable, y
sta VUCOLORPOS, y
dey
bpl :-
.endif
; Draw the VU meter on right, then draw its mirror on the left
DrawVU: lda #<VUPOS1
sta zptmp
lda #>VUPOS1
sta zptmp+1
lda #<VUPOS2
sta zptmpB
lda #>VUPOS2
sta zptmpB+1
ldy #0
ldx #MAX_VU-1
vuloop: lda #VUSYMBOL
cpy VU ; If we're at or below the VU value we use the
bcc :+ ; VUSYMBOL to draw the current char else we use
lda #MEDIUMSHADE ; the partial shade symbol
: sta (zptmp),y ; Store the char in screen memory
sta tempOutput
tya
pha ; Save Y
txa
tay ; Move X into Y
lda tempOutput
sta (zptmpB), y
pla
tay
iny
dex
cpy #MAX_VU
bcc vuloop
rts
;-----------------------------------------------------------------------------------
; Multiply Multiplies X * Y == ResultLo/ResultHi
;-----------------------------------------------------------------------------------
; X 8 bit value in
; Y 8 bit value in
;
; Apparent credit to Leif Stensson for this approach!
;-----------------------------------------------------------------------------------
Multiply:
stx resultLo
sty resultHi
lda #0
ldx #8
lsr resultLo
mloop: bcc no_add
clc
adc resultHi
no_add: ror
ror resultLo
dex
bne mloop
sta resultHi
rts
;-----------------------------------------------------------------------------------
; DrawSquare
;-----------------------------------------------------------------------------------
; Draw a square on the screen buffer using PETSCII graphics characters. Each corner
; get's a special PETSCII corner character and the top and bottom and left/right
; sides are specified as separate characters also.
;
; Does not draw the color chars on the 64, expects those to be filled in by someone
; or somethig else, as it slows things down if not strictly needed.
;
; SquareX - Arg: X pos of square
; SquareY Arg: Y pos of square
; Width Arg: Square width Must be 2+
; Height Arg: Square Height Must be 2+
;-----------------------------------------------------------------------------------
DrawSquare: ldx SquareX
ldy SquareY
lda Height ; Early out - do nothing for less than 2 height
cmp #2
bpl :+
rts
:
lda Width ; Early out - do nothing for less than 2 width
cmp #2
bpl :+
rts
:
lda #TOPLEFTSYMBOL ; Top Left Corner
jsr OutputSymbolXY
lda #HLINE1SYMBOL ; Top Line
sta lineChar
lda Width
sec
sbc #2 ; 2 less due to start and end chars
cmp #1
bmi :+
inx ; start one over after start char
jsr DrawHLine
dex ; put x back where it was
:
lda #VLINE1SYMBOL ; Otherwise draw middle vertical lines
sta lineChar
lda Height
sec
sbc #2
cmp #1
bmi :+
iny
jsr DrawVLine
; dey ; Normally post-dec Y to fix it up, but not needed here
: ; because Y is loaded explicitly below anyway
lda SquareX
clc