In this post, I’ll continue the two-digit
numeric display. If you miss it, click here and check it out. To finish this
project, we just need to show the numbers in the matrix-pixel (the black-box in
the 6502 emulator). To kickstart, our instructor gave us one example of how to
display graphs, which was a lot helpful.
The first thing that I’ve noticed was the
bitmap table at the bottom. So, I mimic it and made ten tables like that to
represent each number (zero to nine). So far, so good! Then I grabbed the logic
to display one digit, and then my nightmares just started. How to place two
graphs (one for each digit)? How to switch from one number to another? How to
reuse code? Where is my coffee?!
To emulate some if-elseif-else statements,
I used jmp (jump). They are all over the place! However, the 6502 limits the
jump range from -127 to 128. That means moving the code-blocks to satisfy all
jumps limit. For example, the “main” moved down, and the “draw_lft” is above
“main.”
Now the last problem is how to select the
correct bitmap when the number changes. I tried arithmetic shift left, multiply
by 64 and others, but no luck. Then I gave up and implemented that long
compare-and-jump. It worked, but this is not elegant at all. I know that using
arithmetic shift left and multiply by 64 works because the bitmap tables are stored
continuously in the memory, so starting from the “num_0” I should be able to
navigate to the correct digit. I just couldn’t deliver it.
Assembly has limitations. And I have it
too. The simple fact that I couldn’t make a simple if-elseif-else statement discouraged
me. Perhaps I have to change my mindset to work with such low-level language.
See you in the next post.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
define WIDTH 8 ; width of graphic | |
define HEIGHT 8 ; height of graphic | |
; zero-page variable locations | |
define NUM_H $10 | |
define NUM_L $11 | |
define POS_GRP_LFT_H $12 | |
define POS_GRP_LFT_L $13 | |
define POS_GRP_RGT_H $14 | |
define POS_GRP_RGT_L $15 | |
define GRP_ROWS_DRAW $16 | |
define GRP_LFT $17 | |
define GRP_RGT $18 | |
lda #$00 | |
reset: | |
sta NUM_H | |
sta NUM_L | |
jmp draw_lft | |
incr_l: | |
ldy NUM_L | |
iny | |
cpy #$0A | |
beq incr_h | |
sty NUM_L | |
jmp draw_lft | |
incr_h: | |
ldy NUM_H | |
iny | |
cpy #$0A | |
beq reset_incr | |
sty NUM_H | |
ldy #$00 | |
sty NUM_L | |
jmp draw_lft | |
reset_incr: | |
lda #$00 | |
jmp reset | |
decr_l: | |
ldy NUM_L | |
dey | |
cpy #$FF | |
beq decr_h | |
sty NUM_L | |
jmp draw_lft | |
decr_h: | |
ldy NUM_H | |
dey | |
cpy #$FF | |
beq reset_decr | |
sty NUM_H | |
ldy #$09 | |
sty NUM_L | |
jmp draw_lft | |
reset_decr: | |
lda #$09 | |
jmp reset | |
draw_lft: | |
lda #$25 ; create a pointer | |
sta POS_GRP_LFT_H ; which points to where | |
lda #$02 ; the graphic should be drawn | |
sta POS_GRP_LFT_L | |
lda #$00 ; number of rows we've drawn | |
sta GRP_ROWS_DRAW | |
ldx #$00 ; index for data | |
ldy #$00 ; index for screen column | |
jmp draw_lft_loop_index | |
draw_rgt: | |
lda #$30 ; create a pointer | |
sta POS_GRP_RGT_H ; which points to where | |
lda #$02 ; the graphic should be drawn | |
sta POS_GRP_RGT_L | |
lda #$00 ; number of rows we've drawn | |
sta GRP_ROWS_DRAW | |
ldx #$00 ; index for data | |
ldy #$00 ; index for screen column | |
jmp draw_rgt_loop_index | |
main: | |
lda $ff ; get a keystroke | |
ldx #$00 ; clear out the key buffer | |
stx $ff | |
cmp #$71 ; handle q -> increment | |
beq incr_l | |
cmp #$77 ; handle w -> decrement | |
beq decr_l | |
jmp main ; if no key pressed loop forever | |
draw_lft_loop: | |
sta (POS_GRP_LFT_H),y | |
inx | |
iny | |
cpy #WIDTH | |
bne draw_lft_loop_index | |
inc GRP_ROWS_DRAW ; increment row counter | |
lda #HEIGHT ; are we done yet? | |
cmp GRP_ROWS_DRAW | |
beq draw_rgt ; ...exit if we are | |
lda POS_GRP_LFT_H ; load pointer | |
clc | |
adc #$20 ; add 32 to drop one row | |
sta POS_GRP_LFT_H | |
lda POS_GRP_LFT_L ; carry to high byte if needed | |
adc #$00 | |
sta POS_GRP_LFT_L | |
ldy #$00 | |
beq draw_lft_loop_index | |
draw_lft_loop_index: | |
lda NUM_H | |
cmp #$00 | |
beq if_lft_loop_a0 | |
cmp #$01 | |
beq if_lft_loop_a1 | |
cmp #$02 | |
beq if_lft_loop_a2 | |
cmp #$03 | |
beq if_lft_loop_a3 | |
cmp #$04 | |
beq if_lft_loop_a4 | |
cmp #$05 | |
beq if_lft_loop_a5 | |
cmp #$06 | |
beq if_lft_loop_a6 | |
cmp #$07 | |
beq if_lft_loop_a7 | |
cmp #$08 | |
beq if_lft_loop_a8 | |
lda num_9,x | |
jmp draw_lft_loop | |
draw_rgt_loop: | |
sta (POS_GRP_RGT_H),y | |
inx | |
iny | |
cpy #WIDTH | |
bne draw_rgt_loop_index | |
inc GRP_ROWS_DRAW ; increment row counter | |
lda #HEIGHT ; are we done yet? | |
cmp GRP_ROWS_DRAW | |
beq main ; ...exit if we are | |
lda POS_GRP_RGT_H ; load pointer | |
clc | |
adc #$20 ; add 32 to drop one row | |
sta POS_GRP_RGT_H | |
lda POS_GRP_RGT_L ; carry to high byte if needed | |
adc #$00 | |
sta POS_GRP_RGT_L | |
ldy #$00 | |
beq draw_rgt_loop_index | |
;;;;; | |
if_lft_loop_a0: | |
lda num_0,x | |
jmp draw_lft_loop | |
if_lft_loop_a1: | |
lda num_1,x | |
jmp draw_lft_loop | |
if_lft_loop_a2: | |
lda num_2,x | |
jmp draw_lft_loop | |
if_lft_loop_a3: | |
lda num_3,x | |
jmp draw_lft_loop | |
if_lft_loop_a4: | |
lda num_4,x | |
jmp draw_lft_loop | |
if_lft_loop_a5: | |
lda num_5,x | |
jmp draw_lft_loop | |
if_lft_loop_a6: | |
lda num_6,x | |
jmp draw_lft_loop | |
if_lft_loop_a7: | |
lda num_7,x | |
jmp draw_lft_loop | |
if_lft_loop_a8: | |
lda num_8,x | |
jmp draw_lft_loop | |
draw_rgt_loop_index: | |
lda NUM_L | |
cmp #$00 | |
beq if_grt_loop_a0 | |
cmp #$01 | |
beq if_grt_loop_a1 | |
cmp #$02 | |
beq if_grt_loop_a2 | |
cmp #$03 | |
beq if_grt_loop_a3 | |
cmp #$04 | |
beq if_grt_loop_a4 | |
cmp #$05 | |
beq if_grt_loop_a5 | |
cmp #$06 | |
beq if_grt_loop_a6 | |
cmp #$07 | |
beq if_grt_loop_a7 | |
cmp #$08 | |
beq if_grt_loop_a8 | |
lda num_9,x | |
jmp draw_rgt_loop | |
;;;;; | |
if_grt_loop_a0: | |
lda num_0,x | |
jmp draw_rgt_loop | |
if_grt_loop_a1: | |
lda num_1,x | |
jmp draw_rgt_loop | |
if_grt_loop_a2: | |
lda num_2,x | |
jmp draw_rgt_loop | |
if_grt_loop_a3: | |
lda num_3,x | |
jmp draw_rgt_loop | |
if_grt_loop_a4: | |
lda num_4,x | |
jmp draw_rgt_loop | |
if_grt_loop_a5: | |
lda num_5,x | |
jmp draw_rgt_loop | |
if_grt_loop_a6: | |
lda num_6,x | |
jmp draw_rgt_loop | |
if_grt_loop_a7: | |
lda num_7,x | |
jmp draw_rgt_loop | |
if_grt_loop_a8: | |
lda num_8,x | |
jmp draw_rgt_loop | |
num_0: | |
dcb $00,$01,$01,$01,$01,$01,$01,$00 | |
dcb $01,$01,$01,$01,$01,$01,$01,$01 | |
dcb $01,$01,$01,$00,$00,$01,$01,$01 | |
dcb $01,$01,$01,$00,$00,$01,$01,$01 | |
dcb $01,$01,$01,$00,$00,$01,$01,$01 | |
dcb $01,$01,$01,$00,$00,$01,$01,$01 | |
dcb $01,$01,$01,$01,$01,$01,$01,$01 | |
dcb $00,$01,$01,$01,$01,$01,$01,$00 | |
num_1: | |
dcb $00,$00,$00,$00,$01,$01,$01,$00 | |
dcb $00,$00,$00,$01,$01,$01,$01,$00 | |
dcb $00,$00,$01,$01,$01,$01,$01,$00 | |
dcb $00,$00,$00,$00,$01,$01,$01,$00 | |
dcb $00,$00,$00,$00,$01,$01,$01,$00 | |
dcb $00,$00,$00,$00,$01,$01,$01,$00 | |
dcb $00,$00,$00,$00,$01,$01,$01,$00 | |
dcb $00,$00,$00,$00,$01,$01,$01,$00 | |
num_2: | |
dcb $00,$01,$01,$01,$01,$01,$01,$00 | |
dcb $01,$01,$01,$01,$01,$01,$01,$01 | |
dcb $01,$01,$01,$00,$00,$01,$01,$01 | |
dcb $00,$00,$00,$00,$01,$01,$01,$00 | |
dcb $00,$00,$01,$01,$01,$00,$00,$00 | |
dcb $01,$01,$01,$00,$00,$00,$00,$00 | |
dcb $01,$01,$01,$01,$01,$01,$01,$01 | |
dcb $01,$01,$01,$01,$01,$01,$01,$01 | |
num_3: | |
dcb $01,$01,$01,$01,$01,$01,$01,$00 | |
dcb $01,$01,$01,$01,$01,$01,$01,$01 | |
dcb $00,$00,$00,$00,$00,$01,$01,$01 | |
dcb $01,$01,$01,$01,$01,$01,$01,$00 | |
dcb $01,$01,$01,$01,$01,$01,$01,$00 | |
dcb $00,$00,$00,$00,$00,$01,$01,$01 | |
dcb $01,$01,$01,$01,$01,$01,$01,$01 | |
dcb $01,$01,$01,$01,$01,$01,$01,$00 | |
num_4: | |
dcb $01,$01,$01,$00,$01,$01,$01,$00 | |
dcb $01,$01,$01,$00,$01,$01,$01,$00 | |
dcb $01,$01,$01,$00,$01,$01,$01,$00 | |
dcb $01,$01,$01,$01,$01,$01,$01,$01 | |
dcb $01,$01,$01,$01,$01,$01,$01,$01 | |
dcb $00,$00,$00,$00,$01,$01,$01,$00 | |
dcb $00,$00,$00,$00,$01,$01,$01,$00 | |
dcb $00,$00,$00,$00,$01,$01,$01,$00 | |
num_5: | |
dcb $01,$01,$01,$01,$01,$01,$01,$01 | |
dcb $01,$01,$01,$01,$01,$01,$01,$01 | |
dcb $01,$01,$01,$00,$00,$00,$00,$00 | |
dcb $01,$01,$01,$01,$01,$01,$01,$00 | |
dcb $01,$01,$01,$01,$01,$01,$01,$01 | |
dcb $00,$00,$00,$00,$00,$01,$01,$01 | |
dcb $01,$01,$01,$01,$01,$01,$01,$01 | |
dcb $00,$01,$01,$01,$01,$01,$01,$00 | |
num_6: | |
dcb $00,$01,$01,$01,$01,$01,$01,$00 | |
dcb $01,$01,$01,$01,$01,$01,$01,$01 | |
dcb $01,$01,$01,$00,$00,$00,$00,$00 | |
dcb $01,$01,$01,$01,$01,$01,$01,$00 | |
dcb $01,$01,$01,$01,$01,$01,$01,$01 | |
dcb $01,$01,$01,$00,$00,$01,$01,$01 | |
dcb $01,$01,$01,$01,$01,$01,$01,$01 | |
dcb $00,$01,$01,$01,$01,$01,$01,$00 | |
num_7: | |
dcb $01,$01,$01,$01,$01,$01,$01,$01 | |
dcb $01,$01,$01,$01,$01,$01,$01,$01 | |
dcb $00,$00,$00,$00,$00,$01,$01,$01 | |
dcb $00,$00,$00,$00,$01,$01,$01,$00 | |
dcb $00,$00,$00,$01,$01,$01,$00,$00 | |
dcb $00,$00,$01,$01,$01,$00,$00,$00 | |
dcb $00,$00,$01,$01,$01,$00,$00,$00 | |
dcb $00,$00,$01,$01,$01,$00,$00,$00 | |
num_8: | |
dcb $00,$01,$01,$01,$01,$01,$01,$00 | |
dcb $01,$01,$01,$01,$01,$01,$01,$01 | |
dcb $01,$01,$01,$00,$00,$01,$01,$01 | |
dcb $00,$01,$01,$01,$01,$01,$01,$00 | |
dcb $00,$01,$01,$01,$01,$01,$01,$00 | |
dcb $01,$01,$01,$00,$00,$01,$01,$01 | |
dcb $01,$01,$01,$01,$01,$01,$01,$01 | |
dcb $00,$01,$01,$01,$01,$01,$01,$00 | |
num_9: | |
dcb $00,$01,$01,$01,$01,$01,$01,$00 | |
dcb $01,$01,$01,$01,$01,$01,$01,$01 | |
dcb $01,$01,$01,$00,$00,$01,$01,$01 | |
dcb $01,$01,$01,$01,$01,$01,$01,$01 | |
dcb $00,$01,$01,$01,$01,$01,$01,$01 | |
dcb $00,$00,$00,$00,$00,$01,$01,$01 | |
dcb $01,$01,$01,$01,$01,$01,$01,$01 | |
dcb $00,$01,$01,$01,$01,$01,$01,$00 |
Comments
Post a Comment