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
|
**************************************************************************
*
* Title: Memory Monitor
*
* Objective: CMPEN 472 Homework 6
*
* Revision: V1.0
*
* Date: Feb. 28, 2025
*
* Programmer: Jacob McDonnell
*
* Company: The Pennsylvania State University
* Department of Computer Science and Engineering
*
* Algorithm: Simple Serial I/O, address poking and modifying
*
* Register Use: A & B to current byte, etc,
* X & Y holds address of strings and length of string,
* D to hold data and address of the memory location to work on.
*
* Memory Use: RAM Locations from $3000 for data,
* RAM Locations from $3100 for program
*
* Input: Serial Port for User Input
*
* Output: Serial Port for String Output
* Memory locations changed if modified by user
*
* Observation: This program will prompt the user to print the contents of a
* memory location and to modify the location with hexadecimal or
* decimal data. If the user wishes, they can type 'QUIT' to exit
* the memory monitor and enter the type writer program.
*
* Note: ON CSM-12C128 board,
*
* Comments: This program is developed and simulated using CodeWarrior
* development software and targeted for Axion
* Manufacturing's CSM-12C128 board running at 24MHz.
*
**************************************************************************
* Parameter Declearation Section
*
* Export Symbols
xdef pgstart ; export 'pgstart' symbol
absentry pgstart ; for assembly entry point
* Symbols and Macros
PORTA equ $0000 ; i/o port A addresses
DDRA equ $0002 ; data direction register for PORTA
PORTB equ $0001 ; i/o port B addresses
DDRB equ $0003 ; data direction register for PORTB
SCIBDH equ $00C8 ; Serial port (SCI) Baud Register H
SCIBDL equ $00C9 ; Serial port (SCI) Baud Register L
SCICR2 equ $00CB ; Serial port (SCI) Control Register 2
SCISR1 equ $00CC ; Serial port (SCI) Status Register 1
SCIDRL equ $00CF ; Serial port (SCI) Data Register
CR equ $0d ; carriage return, ASCII 'Return' key
LF equ $0a ; line feed, ASCII 'next line' character
NULL equ $00 ; NULL Terminator character
**************************************************************************
* Data Section: address used [ $3000 to $30FF ] RAM Memory
*
org $3000 ; Reserved RAM memory starting address
; for Data for CMPEN 472 class
TESTER dc.w $0005 ; Memory location to test showing data
; and writing data.
buffer ds.b $0010 ; Array of 16 bytes to read a string
dc.b NULL ; NULL terminated
lenBuf dc.w $0010 ; Length of buffer array
buffer2 ds.b $0010 ; Array of 16 bytes for reading and reversal
dc.b NULL ; NULL terminated
lenBuf2 dc.w $0010 ; length of buffer2
*
* There is a section Data Section at the end of the file
**************************************************************************
* Program Section: address used [ $3100 to $3FFF ] RAM Memory
*
org $3100 ; Program start address, in RAM
pgstart lds #$3100 ; initialize the stack pointer
ldaa #%11111111 ; Seven Segment display on PORTB
staa DDRB
ldaa #$0C ; Enable SCI port Tx and Rx units
staa SCICR2 ; disable SCI interrupts
ldd #$0001 ; Set SCI Baud Register = $0001 => 1.5M baud at 24MHz (for simulation)
std SCIBDH ; SCI port baud rate change
ldx #msg ; Load the address of the welcome message into X
jsr WriteString ; Write the string to the serial console
mainLoop
ldx #buffer ; Load the address of the buffer into X
ldy lenBuf ; Load the length of the buffer into Y
jsr Zeros ; Fill the buffer with zeros
ldaa #'>' ; Load '>' into A
jsr putchar ; Jump to putchar to write to console
ldaa #' ' ; Load space character into A
jsr putchar ; Jump to putchar to write to console
ldx #buffer ; Load the address of the buffer into X
ldy lenBuf ; Load the length of the buffer into Y
jsr ReadString ; Read a line from the serial console
ldx #buffer ; Load the address of the buffer into X
jsr CheckInput ; Jump to CheckInput to parse the user input
bra mainLoop ; Loop back to mainLoop always
TypeWriter ldx #twMsg ; Load Type Writer welcome message address
jsr WriteString ; Jump to WriteString to write message to serial
twReadLoop jsr getchar ; Read Character from Serial
beq twReadLoop ; While Character == 0, branch to twReadLoop
jsr putchar ; Write Character back to terminal
staa PORTB ; Write Character to PORTB
bra twReadLoop ; Branch always to twReadLoop
**************************************************************************
* Subroutine Section: address used [ $3100 to $3FFF ] RAM Memory
*
;*************************************************************************
; CheckInput subroutine
;
; This subroutine will check the input string and match the option.
;
; Input: Address of null terminated string in X.
; Output: No Output, Control flow changed to proper subroutine.
; Registers in use: X for the address of the string, A & B to read characters from
; from the string.
; Memory locations in use: Memory Address for serial line, address of the string
;
; Comments: This subroutine will not return a value, it will jump to the proper subroutine
; based on the input given. If "QUIT" is the command, this subroutine will jump
; to TypeWriter which will not return and needs to be restarted to exit.
;
CheckInput
pshx ; Save X to the stack
pshd ; Save D to the stack
ldaa 1,x+ ; Read Character from the string in X
cmpa #'W' ; Compare Character to 'W'
lbeq cWrite ; If A == 'W', branch to cWrite
cmpa #'Q' ; Compare A to 'Q'
lbeq cTypeWrite ; If A == 'Q', branch to cTypeWrite
cmpa #'M' ; Compare A to 'M'
beq cMDCommand ; If A != 'M', Check if LD command
cmpa #'L' ; Compare A to 'L'
lbeq cLDCommand ; If A != 'L', Check if GO command
cmpa #'G' ; Compare A to 'G'
beq cGoTo ; If A != 'G', Check if L command
cmpa #'S' ; Compare A to 'S'
lbne cUnknownCMD ; If A != 'S', Command unknown
ldaa 0,x ; Load next character but don't increment
cmpa #'$' ; Compare A to '$'
lbne cUnknownCMD ; If A != '$', branch to unknown command
jsr ReadHex ; ReadHex to Read the memory Address
lbeq cBadAddr ; If Z == 1, branch to cBadAddr
exg Y,X ; Exchange Y for X
pshx ; Save X to the stack
ldx #buffer ; Load address of buffer into X
ldy lenBuf ; Loadd length of the buffer into Y
jsr Zeros ; Fill buffer with Zeros
pulx ; Restore X from the stack
lbra cDone ; branch always to cDone
cGoTo ldaa 1,x+ ; Load next character
cmpa #'O' ; Compare to 'O'
lbne cUnknownCMD ; If A != 'S', Command unknown
ldaa 0,x ; Load next character but don't increment
cmpa #'$' ; Compare A to '$'
lbne cUnknownCMD ; If A != '$', branch to unknown command
jsr ReadHex ; ReadHex to Read the memory Address
lbeq cBadAddr ; If Z == 1, branch to cBadAddr
jsr 0,y ; Launch address in Y
lbra cNoPrint ; Done with command
cMDCommand ldaa 1,x+ ; Load next character into A
cmpa #'D' ; Compare do 'D' character
lbne cUnknownCMD ; If A != 'D', unknown command
ldaa 0,x ; Load next character but don't increment
cmpa #'$' ; Compare A to '$'
lbne cUnknownCMD ; If A != '$', branch to unknown command
jsr ReadHex ; ReadHex to Read the memory Address
lbeq cBadAddr ; If Z == 1, branch to cBadAddr
exg Y,D ; Exchange Y and D
pshd ; Save D to the stack
mSkipSpaces ldaa 1,x+ ; Load next character into A
lbeq cBadData ; If A == 0, branch to bad Data (no data given)
cmpa #' ' ; Compare A to space character
lbeq mSkipSpaces ; While A == ' ', loop to skip spaces
cmpa #'$' ; Compare A to '$'
lbne cBadData ; If A != '$', branch to cBadData
dex ; Decrement X by 1
jsr ReadHex ; Jump to ReadHex to read hex data
lbeq cBadData ; If Z == 1, branch to cBadData
puld ; Restore D from the stack
exg D,X ; Exchange D and X
pshd ; Save D to the stack, cBadData usually has 2 D's on the stack
cpy #2 ; Compare Y to 2
blo cBadData ; No distances below 2 bytes
puld ; Restore D from the stack
jsr MDCommand ; Jump to MDCommand
lbra cNoPrint ; Done with command
cLDCommand ldaa 1,x+ ; Load next character into A
cmpa #'D' ; Compare do 'D' character
bne cUnknownCMD ; If A != 'D', unknown command
ldaa 0,x ; Load next character but don't increment
cmpa #'$' ; Compare A to '$'
bne cUnknownCMD ; If A != '$', branch to unknown command
jsr ReadHex ; ReadHex to Read the memory Address
beq cBadAddr ; If Z == 1, branch to cBadAddr
exg Y,D ; Exchange Y and D
pshd ; Save D to the stack
lSkipSpaces ldaa 1,x+ ; Load next character into A
beq cBadData ; If A == 0, branch to bad Data (no data given)
cmpa #' ' ; Compare A to space character
beq lSkipSpaces ; While A == ' ', loop to skip spaces
cmpa #'$' ; Compare A to '$'
bne cBadData ; If A != '$', branch to cBadData
dex ; Decrement X by 1
jsr ReadHex ; Jump to ReadHex to read hex data
beq cBadData ; If Z == 1, branch to cBadData
puld ; Restore D from the stack
exg D,X ; Exchange D and X
pshd ; Save D to the stack, cBadData usually has 2 D's on the stack
cpy #1 ; Compare Y to 1
blo cBadData ; No distances below 2 bytes
puld ; Restore D from the stack
jsr LDCommand ; Jump to MDCommand
bra cNoPrint ; Done with command
cWrite ldaa 0,x ; Load next character but don't increment
cmpa #'$' ; Compare A to '$'
bne cUnknownCMD ; If A != '$', branch to unknown command
jsr ReadHex ; ReadHex to Read the memory Address
beq cBadAddr ; If Z == 1, branch to cBadAddr
exg Y,D ; Exchange Y and D
pshd ; Save D to the stack
skipSpaces ldaa 1,x+ ; Load next character into A
beq cBadData ; If A == 0, branch to bad Data (no data given)
cmpa #' ' ; Compare A to space character
beq skipSpaces ; While A == ' ', loop to skip spaces
cmpa #'$' ; Compare A to '$'
beq cHexData ; If A == '$', branch to cHexData
dex ; Decrement X by 1
jsr ReadDecimal ; Jump to ReadDecimal
beq cBadData ; If Z == 1, branch to cBadData
bra cWriteData ; Branch always to cWriteData
cHexData dex ; Decrement X by 1
jsr ReadHex ; Jump to ReadHex to read hex data
beq cBadData ; If Z == 1, branch to cBadData
cWriteData puld ; Restore D from the stack
exg D,X ; Exchange D and X
sty x ; Write data in Y to memory in X
cDone jsr PrintMem ; Jump to PrintMem to print the contents of the location
cNoPrint puld ; Restore D from the stack
pulx ; Restore X from the stack
rts ; Return to caller
cBadAddr ldx #badAddr ; Load the address of badAddr into X
jsr WriteString ; Write badAddr to serial console
puld ; Restore D from the stack
pulx ; Restore X from the stack
rts ; Return to caller
cBadData ldx #badData ; Load the address of badData into X
jsr WriteString ; Write badData to serial console
puld ; Restore D from the stack
puld ; Restore D from the stack
pulx ; Restore X from the stack
rts ; Return to caller
cUnknownCMD ldx #badCom ; Load the address of badCom into X
jsr WriteString ; Write badCom to the serial console
puld ; Restore D from the stack
pulx ; Restore X from the stack
rts ; Return to caller
cTypeWrite ldaa 1,x+ ; Load next character from X
cmpa #'U' ; Compare A to 'U'
bne cUnknownCMD ; If A != 'U', branch to unknown command
ldaa 1,x+ ; Load next character from X
cmpa #'I' ; Compare A to 'I'
bne cUnknownCMD ; If A != 'I', branch to unknown command
ldaa 1,x+ ; Load next character from X
cmpa #'T' ; Compare A to 'T'
bne cUnknownCMD ; If A != 'T', branch to unknown command
ldaa 1,x+ ; Load next character from X
bne cUnknownCMD ; If A != 0, branch to unknown command
jmp TypeWriter ; Jump to TypeWriter
;*************************************************************************
; ReadHex subroutine
;
; This subroutine will read an ASCII string of a number in hex and convert it to
; its value.
;
; Input: A memory address in register X.
; Output: The value of the hex number in the Y register, and any errors printed
; to the serial line. Zero bit is set if error occurs.
; Registers in use: X for the address of the contents and for a buffer while printing,
; D for multiplication, B for the character, Y for output value.
; Memory locations in use: Memory Address for serial line, address of the string
;
; Comments: This subroutine will return the value in the Y register, and if an error occurs,
; the Zero bit in the CCR will be set.
;
ReadHex
pshd ; Save D to the stack
ldy #0 ; Clear Y register
ldab 1,x+ ; Read character from X into B, add 1 to X
cmpb #'$' ; Compare B to '$'
bne rHError ; If B != '$', jump to error, not hex data
rHLoop ldab 1,x+ ; Read Next character from X
beq rHDone ; If B == 0, exit loop
cmpb #' ' ; Compare B to space character
beq rHDone ; If B == ' ', exit loop
cmpb #'0' ; Compare B to '0' character
blt rHError ; If B < '0', bad address, exit loop
cmpb #'9' ; Compare B to '9' character
bhi rHAlpha ; If B > '9', check if 'A'-'F' characters
subb #'0' ; Subtract '0' from B to get true value
pshb ; Save B to the stack
ldd #16 ; load 16 into D
emul ; Multiply Y and D
exg d,y ; Transfer data from D to Y
pulb ; Restore b from the stack
aby ; Add B to Y
bra rHLoop ; Branch always to rHLoop
rHAlpha cmpb #'A' ; Compare B to 'A' character
blt rHError ; If B < 'A', bad address, exit loop
cmpb #'F' ; Compare B to 'F' character
bhi rHError ; If B > 'F', invalid data, error out
subb #'A' ; Subtract 'A' from B to get true value
addb #$A ; Add $A to B to account for offet
pshb ; Save B to the stack
ldd #16 ; load 16 into D
emul ; Multiply Y and D
exg d,y ; Transfer data from D to Y
pulb ; Restore b from the stack
aby ; Add B to Y
bra rHLoop ; Branch always to rHLoop
rHDone clra ; clear A accumulator
tap ; Transfer A into CCR to clear zero bit
puld ; Restore D from the stack
rts ; Return to caller
rHError ldaa #4 ; Load 4 into A to set zero bit in CCR
tap ; Transfer A into CCR to set zero bit and warn error
puld ; Restore D from the stack
rts ; Return to caller
MDCommand
pshx ; Save X to the stack
pshy ; Save Y to the stack
pshd ; Save D to the stack
pshx ; Save X to the stack
pshy ; Save Y to the stack
tfr x,d ; Copy X to D
ldx #buffer ; Load the address of the buffer into X
ldy lenBuf ; Load the length of the buffer into Y
jsr Zeros ; Fill the buffer with Zeros
ldy #buffer ; Load the address of the buffer into Y
jsr PrintHexWord ; Print the data
ldaa #' ' ; Load space character into A
jsr putchar ; Print space to console
puly ; Restore Y from the stack
pulx ; Restore X from the stack
ldd #0 ; Clear D
MDLoop cpy #0 ; Compare Y to 0
beq MDDone ; If Y == 0, done
dey ; Decrement Y by 1
dey ; Decrement Y by 1
ldab 1,x+ ; Load data from X into B
jsr PrintHexByte ; Print the data
ldab 1,x+ ; Load data from X into B
jsr PrintHexByte ; Print the data
tfr y,a ; Transfer lower byte Y to A
anda #15 ; And A with 15, basically modulus 16
bne MDLoop ; If A & 15 != 0, no newline
ldaa #CR ; Load CR into A
jsr putchar ; Print CR to serial console
ldaa #LF ; Load LF into A
jsr putchar ; Print LF to serial console
cpy #0 ; Compare Y to 0
beq MDDone ; If Y == 0, done
pshx ; Save X to the stack
pshy ; Save Y to the stack
tfr x,d ; Copy X to D
ldx #buffer ; Load the address of the buffer into X
ldy lenBuf ; Load the length of the buffer into Y
jsr Zeros ; Fill the buffer with Zeros
ldy #buffer ; Load the address of the buffer into Y
jsr PrintHexWord ; Print the data
ldaa #' ' ; Load space character into A
jsr putchar ; Print space to console
puly ; Restore Y from the stack
pulx ; Restore X from the stack
bra MDLoop ; Loop
MDDone ldaa #CR ; Load CR into A
jsr putchar ; Print CR to serial console
ldaa #LF ; Load LF into A
jsr putchar ; Print LF to serial console
puld ; Restore D from the stack
puly ; Restore Y from the stack
pulx ; Restore X from the stack
rts ; Return
LDCommand
pshx ; Save X to the stack
pshy ; Save Y to the stack
pshd ; Save D to the stack
iny ; Add 1 to y for loop
LDLoop dbeq y,LDDone ; Decrement y, y == 0, done
jsr GetCharLoop ; Read Character from serial
cmpa #CR ; Compare to CR
bne NotCR ; If A != CR, not CR
jsr putchar ; Echo to serial
jsr GetCharLoop ; Read Character from serial
NotCR cmpa #LF ; Compare to LF
bne NotNewline ; If not LF, skip next read
jsr putchar ; Echo to serial
jsr GetCharLoop ; Read Character from serial
cmpa #CR ; Compare to CR
bne NotNewline ; If not CR, skip next read
jsr GetCharLoop ; Read Character from serial
NotNewline jsr putchar ; Echo to serial
jsr IsValidHex ; Check if valid hex
beq LDInvalid ; If Z == 1, invalid
tab ; Transfer A to B
jsr GetCharLoop ; Read Character from serial
cmpa #CR ; Compare to CR
bne NotCR2 ; If A != CR, not CR
jsr putchar ; Echo to serial
jsr GetCharLoop ; Read Character from serial
NotCR2 cmpa #LF ; Compare to LF
bne NotNewline2 ; If not LF, skip next read
jsr putchar ; Echo to serial
jsr GetCharLoop ; Read Character from serial
cmpa #CR ; Compare to CR
bne NotNewline2 ; If not CR, skip next read
jsr GetCharLoop ; Read Character from serial
NotNewline2 jsr putchar ; Echo to serial
jsr IsValidHex ; Check if valid hex
beq LDInvalid ; If Z == 1, invalid
exg a,b ; Swap A and B
jsr ReadHexByte ; Read ASCII Hex into data
staa 1,x+ ; Store byte into memory
bra LDLoop ; Loop
LDDone ldaa #CR ; Load CR into A
jsr putchar ; Print to serial
ldaa #LF ; Load LF into A
jsr putchar ; Print to serial
puld ; Restore D from the stack
puly ; Restore Y from the stack
pulx ; Restore X from the stack
rts ; Return
LDInvalid ldaa #CR ; Load CR into A
jsr putchar ; Print to serial
ldaa #LF ; Load LF into A
jsr putchar ; Print to serial
ldx #badData ; Load the address of bad data string
jsr WriteString ; Write string to serial
puld ; Restore D from the stack
puly ; Restore Y from the stack
pulx ; Restore X from the stack
rts ; Return
GetCharLoop
jsr getchar ; Read character from serial
beq GetCharLoop ; If 0, loop
rts ; Return
IsValidHex
cmpa #'0' ; Compare A to '0'
blo InvalidHex ; A < '0', invalid
cmpa #'9' ; Compare A to '9'
bls ValidHex ; A <= '9', valid hex
cmpa #'A' ; Compare A to 'A'
blo InvalidHex ; A < 'A', invalid
cmpa #'F' ; Compare A to 'F'
bhi InvalidHex ; A > 'F', invalid
ValidHex andcc #%11111011 ; Clear Zero Bit
rts ; Return
InvalidHex orcc #%00000100 ; Set the Zero Bit
rts ; Return
;*************************************************************************
; ReadDecimal subroutine
;
; This subroutine will read an ASCII string of a number in decimal and convert it to
; its value.
;
; Input: A memory address in register X.
; Output: The value of the number in the Y register, and any errors printed
; to the serial line. Zero bit is set if error occurs.
; Registers in use: X for the address of the contents and for a buffer while printing,
; D for multiplication, B for the character, Y for output value.
; Memory locations in use: Memory Address for serial line, address of the string
;
; Comments: This subroutine will return the value in the Y register, and if an error occurs,
; the Zero bit in the CCR will be set.
;
ReadDecimal
pshd ; Save D to the stack
ldy #0 ; Clear Y register
dHLoop ldab 1,x+ ; Read Next character from X
beq dHDone ; If B == 0, exit loop
cmpb #' ' ; Compare B to space character
beq dHDone ; If B == ' ', exit loop
cmpb #'0' ; Compare B to '0' character
blt dHError ; If B < '0', bad address, exit loop
cmpb #'9' ; Compare B to '9' character
bhi dHError ; If B > '9', check if 'A'-'F' characters
subb #'0' ; Subtract '0' from B to get true value
pshb ; Save B to the stack
ldd #10 ; load 10 into D
emul ; Multiply Y and D
exg d,y ; Transfer data from D to Y
pulb ; Restore b from the stack
aby ; Add B to Y
bra dHLoop ; Branch always to rHLoop
dHDone clra ; clear A accumulator
tap ; Transfer A into CCR to clear zero bit
puld ; Restore D from the stack
rts ; Return to caller
dHError ldaa #4 ; Load 4 into A to set zero bit in CCR
tap ; Transfer A into CCR to set zero bit and warn error
puld ; Restore D from the stack
rts ; Return to caller
;*************************************************************************
; PrintMem subroutine
;
; This subroutine will print the address and contents of the given memory location.
;
; Input: A memory address in register X.
; Output: The memory address in Hex and the contents in binary, hex, & decimal
; on the serial output.
; Registers in use: X for the address of the contents and for a buffer while printing,
; D for the contents of the location, Y for another buffer address while
; reversing strings and printing.
; Memory locations in use: Memory Address for serial line, address of the string
;
; Comments: This subroutine requires the serial console to be setup before calling.
;
PrintMem
pshy ; Save Y to the Stack
pshd ; Save D to the Stack
exg x,d ; Copy Address from X to D to print
ldy #buffer ; Load the address of buffer into Y
jsr PrintHexWord ; Print the Address in Hex
exg d,x ; Copy Address from D to X
ldaa #'=' ; Load the '=' character into A
jsr putchar ; Print the character to the serial
ldaa #'>' ; Load the '>' character into A
jsr putchar ; Print the character to the serial
ldd x ; Load data from address in X into D
ldx #buffer ; Load the address of the buffer into X
ldy lenBuf ; Load the length of the buffer into Y
jsr Zeros ; Fill the buffer with Zeros
jsr PrintBinaryWord ; Print the binary word to the serial
psha ; Save A to the stack
ldaa #' ' ; Load Space character into A
jsr putchar ; Print Space character to the serial
jsr putchar ; Print Space character to the serial
pula ; Restore A from the stack
ldy #buffer ; Load the address of the buffer into Y
jsr PrintHexWord ; Prin the hex representation of the word to serial
psha ; Save A to the stack
ldaa #' ' ; Load Space character into A
jsr putchar ; Print Space character to the serial
jsr putchar ; Print Space character to the serial
pula ; Restore A from the stack
ldx #buffer ; Load the address of the buffer into X
ldy lenBuf ; Load the length of the buffer into Y
jsr Zeros ; Fill the buffer with Zeros
ldy #buffer ; Load the address of the buffer into Y
jsr PrintDecimalWord; Prin the decimal representation of the word to serial
ldaa #CR ; Load the carriage return character into A
jsr putchar ; Print the character to serial
ldaa #LF ; Load the line feed character into A
jsr putchar ; Print the character to serial
puld ; Restore D from the stack
puly ; Restore Y from the stack
rts ; Return to caller
;*************************************************************************
; strrev subroutine
;
; This subroutine will reverse a string from one buffer into another.
;
; Input: Address of null terminated string in X, address of a large enough
; buffer in Y.
; Output: The string in X reversed in Y.
; Registers in use: X for the address of the string, Y for the address of the buffer,
; A to read characters from the string.
; Memory locations in use: Memory Address for serial line, address of the string & buffer
;
; Comments: This subroutine will not check that the output buffer is large enough, that
; is the job of the caller.
;
strrev
pshx ; Save X to the stack
pshy ; Save Y to the stack
psha ; Save A to the stack
revLoop ldaa 1,y- ; Load Character from Y into A, decrement Y
beq revDone ; If Character is 0, exit loop
staa 1,x+ ; Save character in address in X, increment X
bra revLoop ; Loop back always
clra ; Set A to Zero
revDone staa 1,x+ ; Copy Null terminator into new string
pula ; Restore A from the stack
puly ; Restore Y from the stack
pulx ; Restore X from the stack
rts ; Return to caller
;*************************************************************************
; PrintBinaryWord subroutine
;
; This subroutine will print a given word of data to the serial in binary.
;
; Input: 1 word of data in register D
; Output: Binary representation of the data on the serial console
; Registers in use: X to count the number of bits written, D for the input, A for characters,
; B for the byte being written.
; Memory locations in use: Memory addresses for serial.
;
; Comments: This subroutine requires serial to be setup and putchar subroutine.
;
PrintBinaryWord
pshx ; Save X to the stack
pshd ; Save D (A:B) to the stack
pshb ; Save B to the stack (we want these bits again later)
tab ; Transfer A to B to get upper byte
ldaa #'%' ; Load '%' into A
jsr putchar ; Print '%' to serial to denote binary number
ldx #16 ; Load 16 into X, since we're printing 16 bits
bPrintLoop rolb ; Rotate MSB of B into C of CCR
tpa ; Copy CCR into A
anda #1 ; and A with 1 to get only LSB
adda #'0' ; Add '0' to A to get ASCII Character
jsr putchar ; Print Character A to serial
dbeq X,bPrintDone ; Decrement X and if X == 0, branch to done
cpx #8 ; Compare X to 8 to check if done with upper byte
bne bPrintLoop ; If X != 8, loop to bPrintLoop
pulb ; Restore B from stack to get lower byte
bra bPrintLoop ; Branch back into loop to print lower byte
bPrintDone puld ; Restore D (A:B) from the stack
pulx ; Restore X from the stack
rts ; Return to caller
PrintHexByte
pshd ; Save D to the stack
pshy ; Save Y to the stack
pshx ; Save X to the stack
clra ; Clear A
ldx #16 ; Load 16 into X
idiv ; D / 16 => X & D
tfr x,a ; Transfer lower byte of X to A
cmpa #10 ; Compare A to 10
bhs IsAlpha ; If A >= 10, IsAlpha
adda #'0' ; Add '0' to A to make character
bra PrintChar ; Print jump to print the character
IsAlpha adda #'A' ; Add 'A' to A to make character
suba #$A ; Subtract 10 from A
PrintChar jsr putchar ; Print character to serial
tba ; Transfer B to A
cmpa #10 ; Compare A to 10
bhs IsAlpha2 ; If A >= 10, IsAlpha2
adda #'0' ; Add '0' to A to make character
bra PrintDone ; Print jump to print the character
IsAlpha2 adda #'A' ; Add 'A' to A to make character
suba #$A ; Subtract 10 from A
PrintDone jsr putchar ; Print character to serial
ldaa #' ' ; Load space into A
jsr putchar ; Print character to serial
pulx ; Restore X from the stack
puly ; Restore Y from the stack
puld ; Restore D from the stack
rts ; Return
ReadHexByte
cmpa #'A' ; Compare A to 'A'
blo RHBNotHex ; If A < 'A', decimal number
suba #7 ; 'A' - '0' = 17, 'A' - '0' - 7 = 10 = $A
RHBNotHex suba #'0' ; Remove offset
pshb ; Save B to the stack
ldab #16 ; 16 to shift digit to left
mul ; A * 16, left shift digit
tba ; Store result in A
pulb ; Restore B from stack
cmpb #'A' ; Compare B to 'A'
blo RHBNotHexB ; If B < 'A', decimal number
subb #7 ; 'A' - '0' = 17, 'A' - '0' - 7 = 10 = $A
RHBNotHexB subb #'0' ; Remove offset
aba ; Add A to B
rts ; Return
;*************************************************************************
; PrintHexWord subroutine
;
; This subroutine will print a given word of data to the serial in binary.
;
; Input: 1 word of data in register D, Buffer Address in Y
; Output: Hexadecimal representation of the data on the serial console
; Registers in use: Y for the address of the buffer, X to count the number of bits
; written and for division, D for the input, A for characters.
; Memory locations in use: Memory addresses for serial.
;
; Comments: This subroutine requires serial to be setup and putchar subroutine.
;
PrintHexWord
pshx ; Save X to the stack
pshd ; Save D (A:B) to the stack
pshy ; Save Y to the stack
cpd #0 ; Compare D to zero
beq hIsZero ; Branch to hIsZero
psha ; Save A to the stack
pshy ; Save Y to the stack
pshx ; Save x to the stack
ldaa #'0' ; Load the '0' character into A
ldx #buffer2 ; Load the address of buffer2 into X
ldy #5 ; Load 5 into Y
jsr memset ; Write '0' to the first 5 bytes in buffer2
pulx ; Restore X from the stack
puly ; Restore Y from the stack
clra ; Set A to zero
staa 0,y ; Load Zero into Y for Null Terminator
pula ; Restore A from the stack
hPrintLoop ldx #16 ; Load 16 in X for division
idiv ; Divide D / 16 to get Hex Digit
cpx #0 ; Compare X to 0
beq hCheck ; If X == 0, branch to check D is zero
hDNotZero cmpb #$0A ; Compare A to $0A
blt hex10 ; If B < $A, branch to hex10
addb #'A' ; Add 'A' to B to get ASCII Character
subb #$0A ; Subtract $A to adjust characters
stab 1,+y ; Save character from B to Y
exg X,D ; Swap values in X and D
bra hPrintLoop ; loop to hPrintLoop
hex10 addb #'0' ; Add '0' to B to get ASCII Character
stab 1,+y ; Save character from B to Y
exg X,D ; Swap values in X and D
bra hPrintLoop ; Loop to hPrintLoop
hCheck cpd #0 ; Compare D to 0
bne hDNotZero ; If D != 0, branch back to hDNotZero
hPrintDone ldaa #'$' ; Load '$' into
staa 1,+y ; Save '$' into buffer in Y to denote Hex
ldx #buffer2 ; Load the address of buffer2 in X
jsr strrev ; Reverse string in Y in buffer in X
jsr WriteString ; Jump to write string to write the number
ldy lenBuf2 ; Load the length of buffer2 into Y
ldx #buffer2 ; Load the address of buffer2 into X
jsr Zeros ; Fill buffer2 with zeros
puly ; Restore Y from the stack
puld ; Restore D (A:B) from the stack
pulx ; Restore X from the stack
rts ; Return to caller
hIsZero ldaa #'$' ; Load '$' character into A
jsr putchar ; Print character to the screen
ldaa #'0' ; Load '0' character into A
jsr putchar ; Print character to the screen
jsr putchar ; Print character to the screen
jsr putchar ; Print character to the screen
jsr putchar ; Print character to the screen
puly ; Restore Y from the stack
puld ; Restore D (A:B) from the stack
pulx ; Restore X from the stack
rts ; Return to caller
;*************************************************************************
; PrintDecimalWord subroutine
;
; This subroutine will print a given word of data to the serial in binary.
;
; Input: 1 word of data in register D, Buffer Address in Y
; Output: Decimal representation of the data on the serial console
; Registers in use: Y for the address of the buffer, X to count the number of bits
; written and for division, D for the input, A for characters.
; Memory locations in use: Memory addresses for serial.
;
; Comments: This subroutine requires serial to be setup and putchar subroutine.
;
PrintDecimalWord
pshx ; Save X to the stack
pshd ; Save D (A:B) to the stack
pshy ; Save Y to the stack
cpd #0 ; Compare D to zero
beq dIsZero ; Branch to hIsZero
psha ; Save A to the stack
pshy ; Save Y to the stack
pshx ; Save x to the stack
ldaa #'0' ; Load the '0' character into A
ldx #buffer2 ; Load the address of buffer2 into X
ldy #5 ; Load 5 into Y
jsr memset ; Write '0' to the first 5 bytes in buffer2
pulx ; Restore X from the stack
puly ; Restore Y from the stack
clra ; Set A to zero
staa 0,y ; Load Zero into Y for Null Terminator
pula ; Restore A from the stack
dPrintLoop ldx #10 ; Load 10 in X for division
idiv ; Divide D / 10 to get Hex Digit
cpx #0 ; Compare X to 0
beq dCheck ; If X == 0, branch to check D is zero
dDNotZero addb #'0' ; Add '0' to B to get ASCII Character
stab 1,+y ; Save character from B to Y
exg X,D ; Swap values in X and D
bra dPrintLoop ; Loop to hPrintLoop
dCheck cpd #0 ; Compare D to 0
bne dDNotZero ; If D != 0, branch back to hDNotZero
dPrintDone ldx #buffer2 ; Load the address of buffer2 in X
jsr strrev ; Reverse string in Y in buffer in X
jsr WriteString ; Jump to write string to write the number
ldy lenBuf2 ; Load the length of buffer2 into Y
ldx #buffer2 ; Load the address of buffer2 into X
jsr Zeros ; Fill buffer2 with zeros
puly ; Restore Y from the stack
puld ; Restore D (A:B) from the stack
pulx ; Restore X from the stack
rts ; Return to caller
dIsZero ldaa #'0' ; Load '0' character into A
jsr putchar ; Print character to the screen
puly ; Restore Y from the stack
puld ; Restore D (A:B) from the stack
pulx ; Restore X from the stack
rts ; Return to caller
;*************************************************************************
; Zeros subroutine
;
; This subroutine will write zeros to every byte in a given array.
;
; Input: Address of an array in X and its length in Y
; Output: Zeros in every byte of an array.
; Registers in use: X for the address of the array, Y for the length, and A for 0
; Memory locations in use: Memory Address of the array
;
; Comments: This subroutine requires serial to be setup and putchar subroutine.
;
Zeros
psha ; Save A to the Stack
clra ; Clear A
zerosLoop staa 1,x+ ; Load A into byte at X
dbne y,zerosLoop ; Decrement Y and loop if Y != 0
pula ; Restore A from the stack
rts ; Return to caller
;*************************************************************************
; memset subroutine
;
; This subroutine will write a given byte to every byte in a given array.
;
; Input: Address of an array in X and its length in Y, the byte in A
; Output: The given byte in every byte of an array.
; Registers in use: X for the address of the array, Y for the length, and A for the given byte
; Memory locations in use: Memory Address of the array
;
; Comments: This subroutine requires serial to be setup and putchar subroutine.
;
memset
staa 1,x+ ; Load A into byte at X
dbne y,memset ; Decrement Y and loop if Y != 0
rts ; Return to caller
;*************************************************************************
; WriteString subroutine
;
; This subroutine will write a given null terminated string to the serial.
;
; Input: Address of null terminated string in X
; Output: Null terminated string written to serial
; Registers in use: X for the address of the string and A for the current byte
; Memory locations in use: Memory Address for serial line, address of the string
;
; Comments: This subroutine requires serial to be setup and putchar subroutine.
;
WriteString
psha ; Save A to the stack
writeLoop ldaa 1,x+ ; Load the byte at addr in X, then add 1
beq doneWrite ; if A == 0, branch to doneWrite
jsr putchar ; Jump to putchar to write byte to serial
bra writeLoop ; branch always to writeLoop
doneWrite pula ; restore A from the stack
rts ; return to caller
;*************************************************************************
; ReadString subroutine
;
; This subroutine will read a string from the serial line to a given address.
;
; Input: Address of an array in X
; Output: Null terminated string in the given array
; Registers in use: X for the address of the string Y for the length of the string,
; and A for the current byte
; Memory locations in use: Memory Address for serial line, address of the string
;
; Comments: This subroutine requires serial to be setup and getchar subroutine.
;
ReadString
psha ; Save accumulator A to the stack
pshy ; Save Y to the stack
pshx ; Save X to the stack
readLoop jsr getchar ; Jump to putchar to write byte to serial
beq readLoop ; While A == 0, loop
cmpa #CR ; If A == CR, exit loop
beq doneRead ; Branch to doneRead if A == CR
cmpa #LF ; If A == LF, exit loop
beq doneRead ; Branch to doneRead if A == CR
staa 1,x+ ; Save the byte to the addr in X, then add 1
jsr putchar ; Write Character back to the terminal
dey ; Decrement Y by 1
beq doneRead ; If Y == 0, no more room, stop reading
bra readLoop ; branch always to readLoop
doneRead ldaa #LF ; Load Line Feed into A
jsr putchar ; Write LF to terminal
pulx ; Restore X from the stack
pulY ; Restore Y from the stack
pula ; restore A from the stack
rts ; return to caller
;*************************************************************************
; putchar subroutine
;
; This subroutine writes a single byte to a serial line
;
; Input: A single ASCII byte in accumulator A
; Output: Sends one character to SCI port
; Registers in use: Accumulator A with input byte
; Memory locations in use: SCISR1 and SCIDRL status and data registers
;
putchar brclr SCISR1,#%10000000,putchar ; wait for transmit buffer empty
staa SCIDRL ; send a character
rts ; Return to caller
;*************************************************************************
; putchar subroutine
;
; This subroutine reads one byte from the SCI port
;
; Input: One byte from the SCI port
; Output: One byte in accumulator A
; Registers in use: Accumulator A for output byte
; Memory locations in use: SCISR1 and SCIDRL status and data registers
;
getchar brclr SCISR1,#%00100000,getchar7 ; If no input on SCI port, return 0
ldaa SCIDRL ; Read one byte from SCI port into A
rts ; Return to caller
getchar7 clra ; Set A to 0
rts ; Return to caller
*
**************************************************************************
* Data Section 2: address used [ $3100 to $3FFF ] RAM Memory
*
badAddr dc.b 'invalid input, address',CR,LF,NULL ; Error message for bad address
badData dc.b 'invalid input, data',CR,LF,NULL ; Error message for bad data
badCom dc.b 'invalid input, command',CR,LF,NULL ; Error message for bad command
; twMsg: welcome message when type writer loads
twMsg dc.b 'Welcome to Type Writer, you may type below.',CR,LF
dc.b 'Restart to enter main menu again.',CR,LF,NULL
; msg: this is the main option menu string
msg dc.b 'S: Show the contents of memory location in word',CR,LF
dc.b 'W: Write the data word (not byte) to memory location',CR,LF
dc.b 'MD: Display the contents of continuous memory locations',CR,LF
dc.b 'LD: Load a block of data to continuous memory locations',CR,LF
dc.b 'GO: Run the program at the specified memory location',CR,LF
dc.b 'QUIT: Quit the main program, run Type writer program.',CR,LF,NULL
end ; last line of the file
|