Skip to main content

Two-digit Numeric Display - Final


Photo by Nick Hillier on Unsplash


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.

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

Popular posts from this blog

Going Faster

Photo by  Anders Jildén  on  Unsplash Today’s topic is compiler optimizations. Besides translating our source code into machine binary executable, the compiler, based on optimization parameters, can also produce faster executables. Just by adding some parameters to the compiler, we can get a better performance or a smaller executable, for example. There are hundreds of those parameters which we can turn on or off using the prefix -f and -fno. However, instead of doing one by one, we can use the features mode using the -O param. It ranges from 0 (no optimization – the default) to 3 (the highest). Using those parameters has a cost —usually, the faster, the larger executable. How does the compiler make it faster if my code is perfect? I’m going to put some methods here, but if you want, here is more detail . Also, bear in mind that most of the optimizations are done in the intermediate representation of the program. So, the examples below are rewritten just to...

Data Input Form

Photo by  Marvin Meyer  on  Unsplash Continuing the Lab 4, we are going to develop the option 2, data input form. The goal is to prompt the user to enter its name, address, city, province and postal code. Also, letting the user use up, down, left, and right arrows to navigate throughout the fields. After finishing the data input, a summary is presented at the end. Using the ROM routines, wasn’t too hard to allow users to type data into the character display. Then, I decided to make the filed names with the same width, 14 characters, limiting the input to 40 characters. So, the user is not allowed to type in the first 14 and after 54 characters. When the user presses enter at the last field, the summary is shown. I could display the fixed message, but I couldn’t copy the inserted data. I’m still working on that, and I’ll update this post as soon I figure it out. It is frustrating for me to spend days in basic problems that could be solved quickly using other langu...