summaryrefslogtreecommitdiff
path: root/programs/utils.s
blob: a6a14f3ea888c5c6009013f5b1a79d21730501ef (plain)
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
; Utility subroutines for SuBAsm.

.org util_data
; Hex character table.
hex_char:
	.byte "0123456789ABCDEF"

.org utils

print_hi:
	and #0		; Reset A.
	sta idx3	; Clear the string index.
	lda #'$'	; Print the hex delimiter.
	jsr charcpy	;
	lda.q idx0	; Get the masked address.
	ldx #$10	; Set digit count to 16.
	jsr print_hex	; Print the address.
	lda.q hex_str	; Get the lower half of the string.
	sta.q strbuf+1	; Save it in the string buffer.
	lda.q hex_str+8	; Get the upper half of the string.
	sta.q strbuf+9	; Save it in the string buffer.
	ldx #$11	; Add 16 to the index.
	stx idx3	;
	lda #':'	; Print a colon.
	jsr charcpy	;
	lda # ' '	; Print a space.
	jsr charcpy	;
	rts		; End of print_hi.

print_lo:
	lda #0		; Reset A.
	sta idx3	; Clear the string index.
pntlo_lp:
	ldx #2		; Set digit count to 2.
	pha #1		; Preserve the nibble offset.
	jsr print_hex	; Print the low nibble offset.
	lda.w (ptr3)	; Get the two digits.
	jsr charcpy	; Copy the first digit.
	lsr #8		; Copy the next digit.
	jsr charcpy	;
	pla #1		; Get the nibble offset back.
	inc		; Increment the offset.
	cmp #$10	; Are we at the last offset?
	bcs pntlo_end	; Yes, so we're done.
pntlo_lp1:
	pha #1		; No, so preserve the nibble offset.
	lda #' '	; Add a space to the string buffer.
	jsr charcpy	;
	pla #1		; Get the nibble offset back.
	jmp pntlo_lp	; Keep looping.
pntlo_end:
	inx		; Increment the index by one.
	lda #0		; Null terminate the string buffer.
	sta strbuf, x	;
	tax		; Reset X.
	lda.d #strbuf	; Print the string buffer.
	jsr print_str	;
	rts		; End of print_lo.

print_chunk:
	ldx #0		; Reset X.
	phy #2		; Preserve the screen buffer index.
	txy		; Copy the byte index to it.
pntchnk_lp:
	and #0		; Reset A.
	ldx #2		; Set the digit count to 2.
	lda (idx0), y	; Get the byte at that address.
	jsr print_hex	; Print the byte.
	lda.w (ptr3)	; Get the two digits.
	jsr charcpy	; Copy the first digit.
	lsr #8		; Copy the next digit.
	jsr charcpy	;
	iny		; Increment the byte index.
	cpy #$10	; Have we read 16 bytes?
	beq pntchnk_end	; Yes, so we're done.
	lda #' '	; No, so add a soace to the string buffer.
	jsr charcpy	;
	jmp pntchnk_lp	; Keep looping.
pntchnk_end:
	ply #2		; Get the screen buffer index back.
	inx		; Increment the index by one.
	and #0		; Null terminate the string.
	sta strbuf, x	;
	tax		; Reset X.
	sta idx3	; Clear the string index.
	rts		; End of print_chunk.


print_hex:
	pha #8		; Preserve the hex value.
	and #0		; Reset A.
	ldb #1		; Set the second pointer
	lda.w #hex_char	; to the start of hex character table.
	jsr set_ptr	;
	inb		; Set the third pointer
	lda.d #hex_str	; to the end of hex string buffer.
	clc		; Do a non carrying add.
	adc #$10	;
	jsr set_ptr	;
	ldb #0		; Reset B.
	pla #8		; Get the hex value back.
pnthex_lp:
	pha #8		; Preserve the hex value.
	and #$F		; Mask the lowest nibble.
	phy #2		; Preserve the screen buffer position.
	tay		; Get the index for the hex digit.
	lda (ptr2), y	; Get the hex digit.
	dec ptr3	; Decrement the string pointer.
	sta (ptr3)	; Save the hex digit character in the string.
	ply #2		; Get back the screen buffer position.
	pla #8		; Get the hex value back.
pnthex_lp1:
	cpx #1		; Is the digit count less than one?
	bcc pnthex_lp2	; Yes, so don't decrement the digit count.
	dex		; No, but was the digit count zero, when decremented?
	beq pnthex_end	; Yes, so we're done.
	jmp pnthex_lp3	; No, so get the next nibble.
pnthex_lp2:
	ldb #1		; Enable auto digit count.
pnthex_lp3:
	lsr #4		; No, but is the next nibble, a zero?
	beq pnthex_lp4	; Yes, so check if auto digit count is enabled.
	jmp pnthex_lp	; No, so print the next digit.
pnthex_lp4:
	cpb #1		; Is auto digit count enabled?
	beq pnthex_end	; Yes, so we're done.
	jmp pnthex_lp	; No, so keep printing more digits.
pnthex_end:
	rts		; End of print_hex.


charcpy:
	ldx idx3	; Get the string index.
	sta strbuf, x	; Save it in the string buffer.
	inc idx3	; Increment the string index.
	rts		; End of charcpy.