In this
section, It is going to show bitmap code in 6502 emulator.
What is
6502?
The 6502
is an 8-bit processor with a 16-bit address bus. Each pointer in memory is
stored in two consecutive memory locations, with the lowest-value byte stored
first;.
The 6502
instruction set consist of a number of single-byte opcodes, each of which is
followed by 0, 1, or 2 bytes of arguments. 6502 Assembly Language uses 3-letter menomics to specify the operation,
and argument syntax to specify the addressing mode.
The emulator
has a text area for data entry, a small bit-mapped screen (32x32 pixels), a
character screen (80x25 characters), a debug area, a memory monitor, and a
message area.
These
controls are available at the top of the screen:
·
Assemble - assembles the code in the text area, placing
the resulting binary machine language code at $0600 and outputting any error
messasges to the message area.
·
Run - runs the assembled code, if it
assembled correctly. While the code is running, this button becomes a Stop
button.
·
Hexdump - shows, in hexadecimal, the values stored in
memory starting at $0600
·
Disassemble - shows a combined hexdump and disassembly of
c
·
SAMPLE CODE
lda #$00 ; set a pointer at $40 to point to $0200
sta $40 ;
Memory location $fe
contains a new random byte on
every instruction.
lda #$02
sta $41 ;
Memory location $ff
contains the ascii code of
the last key pressed.
lda #$07 ; colour
ldy #$00 ;
set index to 0
loop: sta ($40),y ; set pixel
iny ; increment index
bne loop ; continue until done the page
inc $41 ; increment the
page
ldx $41 ; get the page
cpx #$06 ; compare with 6
bne loop ; continue until done all pages
|
TYA
Tya
is one of resister transfers. It means transfer y to a, so when the increased
index of y, a also increased.
When
we see the result of below code, same colour patern repeated 2 times with 16
colours because there are 32 pixels but colour are only 16.
lda #$00 ; set a pointer at $40 to point to $0200
sta $40 ;
Memory location $fe
contains a new random byte on
every instruction.
lda #$02
sta $41 ;
Memory location $ff
contains the ascii code of
the last key pressed.
lda #$07 ; colour
ldy #$00 ;
set index to 0
loop: tya ; add tya
sta
($40),y ; set pixel
iny ; increment index
bne loop ; continue until done the page
inc $41 ; increment the
page
ldx $41 ; get the page
cpx #$06 ; compare with 6
bne loop ; continue until done all pages
|
Lsr
lsr is one of bit operations. Lsr is
logical shift right. Therefore bit move to right.
Y
|
A
|
0000
0001
0010
0011
|
0000
0000
0001
0001
|
Therfore, same colour is repeated two
times. The colour line looks more wide.
When lsr is repeated the colour line is
wider and wider. When lsr is repeated 5 times, it look like one line.
lda #$00 ; set a pointer at $40 to point to $0200
sta $40 ;
Memory location $fe
contains a new random byte on
every instruction.
lda #$02
sta $41 ;
Memory location $ff
contains the ascii code of
the last key pressed.
lda #$07 ; colour
ldy #$00 ;
set index to 0
loop: tya ; add tya
lsr
; add lsr
sta
($40),y ; set pixel
iny ; increment index
bne loop ; continue until done the page
inc $41 ; increment the
page
ldx $41 ; get the page
cpx #$06 ; compare with 6
bne loop ; continue until done all pages
|
<5times repeate>
|
Asl
asl is one of the bit operations. It is arithmetic
shift left so, when it is added colour number 2,4,6,8,10,12,16 is works.
Y
|
A
|
0000
0001
0010
0011
|
0000
0010
0100
0110
|
lda #$00 ; set a pointer at $40 to point to $0200
sta $40 ;
Memory location $fe
contains a new random byte on
every instruction.
lda #$02
sta $41 ;
Memory location $ff
contains the ascii code of
the last key pressed.
lda #$07 ; colour
ldy #$00 ;
set index to 0
loop: tya ; add tya
asl
; add asl
sta
($40),y ; set pixel
iny ; increment index
bne loop ; continue until done the page
inc $41 ; increment the
page
ldx $41 ; get the page
cpx #$06 ; compare with 6
bne loop ; continue until done all pages
|
Iny
lny is increment y. when it used one time
it looks like yellow. However, when it is repeated with even number it looks
like yellow and black pattern. When it is odd number, when it is completed with
yellow colour the loop condition and the loop can ended.
lda #$00 ; set a pointer at $40 to point to $0200
sta $40 ;
Memory location $fe
contains a new random byte on
every instruction.
lda #$02
sta $41 ;
Memory location $ff
contains the ascii code of
the last key pressed.
lda #$07 ; colour
ldy #$00 ;
set index to 0
loop: sta ($40),y ; set pixel
iny ;
increment index
bne loop ; continue until done the page
inc $41 ; increment the
page
ldx $41 ; get the page
cpx #$06 ; compare with 6
bne loop ; continue until done all pages
|
< Result – even numbers repeated>
|
Write code to draw a green line across the top
of the bitmap screen and a blue line across the bottom.
define POINTER $10
define POINTER_H $11 lda #$00 ; set pointer to $0200 sta POINTER lda #$02 sta POINTER_H ldy #$00 ; set index to 00 lda #$05 ; set colour to green top: sta ($10),y ; set pixel iny ; increment y index cpy #$20 ; compare y value with 20. bne top ; if is not meet the condition go back top lda #$05 ; sta POINTER_H ; set pointer to $0500 lda #$06 ; set colour to blue ldy #$e0 ; set index y bottom: sta ($10), y ; set pixel iny ; increment bne bottom ; if it is not meet the condition go back bottom. |
Extend the previous code to draw a yellow line down the left side of the screen and a purple line down the right side.
lda #$02
sta POINTER_H ; set pointer $0200 ldy #$00 ; set index left_right: lda #$07 ; set colour yellow sta ($10),y ; set pixel tax ; transfer a to x tya ; transfer y to a clc ; clear c flag adc #$1f ; add with #$if tay ; transfer a to t txa ; transfer x to a lda #$04 ; set colour purple sta ($10),y ; set pixcel iny ;increment index bne left_right inc $11 ; increment page ldx $11 ; get the page cpx #$06 ; compare with page number 6 bne left_right |
When I did this work it was so confusing. I used
to think with decimal number but this is machine language. Its base is binary. So
do think with binary or hex.
No comments:
Post a Comment