Thursday, January 30, 2020

Week3 Reflection - Addressing Modes



Addressing Modes

The 6502 uses a 16-bit address. There are 65536 bytes of memory available to the processor.
The memory location represented as $0000 - $ffff. There are 13 ways to refer to these memory locations.

1.Accumulaor

They are eight bits and not located in memory proper. They are briefly rest here before being sent to another destination. When you use these there are no additional data are required.
ASL  : shift left one bit
C <- [76543210]  <- 0
LSR  : shift one bit right
0 > [76543210] ->c
ROL  : Rotate one bit left
        C -< [76543210] <- c
ROR  : Rotate one bit Right
           C -> [76543210] ->c

2.Absolute

The value given is the address (16-bits) of a memory location that contains the 8-bit value to be used as constant.
LDA $06d3
STA $0200

3/4.Absolute, X  OR Absolute, Y

The final address is found by taking the given address as a base and adding the current value of the X or Y resister to it as an offset.
LDA $F453, X : where x contains 3
So, load the accumulator with the contents of address $f453 + 3= $f456.

5.Immediate

The value given is a number to be used immediately by the instruction.
LDA #$99 loads the value $99 into the accumulator.

6.Implied

Many instructions are only one byte in length and do not reference memory.
CLC – Clear the carry flag
DEX – Decrement the X resister by one
TYT – Transfer the y resister to the accumulator
RTS – Return from the Subroutine
      It pulls the top two bytes off the stack and transfers program control to that address+1. Subroutine invoked via JSR which pushed the address -1.

7.Indirect

Indirect addressing uses an absolute address to look up another address.
JMP($00f0) : jumps to the location pointed to by addresses $f0 and $f1. So PC=$cc01

LDA #$01
STA $f0
LDA #$cc
STA $f1
JMP ($00f0) ;dereferences to $cc01

8.X, Indirect

LDA ($05, X)
Take the zero page address, add the value of the X register to it, then use that to look up a two-byte address.
LDX #$01 ; x = $01
LDA #$05
STA $01
LDA #$07
STA $02
LDY #$0A
STY $0705
LDA($00, X) ; think ($00,X) as ( $00 + x) so this simplified to ($01)

9.Indirect,Y

LDA ($10), Y
Find the 16-bit address contained in the given location. Add to that address the contents of the Y
resister. Fetch the value stored at that address. It let’s you have easy access to many locations very quickly by just changing the Y resister or the pointer. The pointer for this addressing mode must be stored in zero page locations.

10.Relative

Relative addressing is used with eight instructions only: BVS, BVC, BCS, BCC, BEQ, BMI, BNE, BPL. They are all “branching” instructions. Branch on: overflow flag set (or cleared), carry flag set (or cleared), equal, minus, not-equal, or plus. Branch if Not-Equal, like the rest of this group, will jump up to 128 addresses forward or backward from where it is or 127 addresses backward (if the result of the most recent activity is "not equal"). Note that these jumps can be a distance of only 128, or 127 back, and they can go in either direction. You specify where the jump should go by giving an address within these boundaries.

11.Zero page

Zero page is a single-byte address. It use only two hex digits, any number between $00 and $ff or a decimal number between 0 and 255. This type of addressing is only the first page of memory is accessible. This is faster, as only one byte needs to be looked up, and takes up less space in the assembled code as well.

12.Zero page X

Add the value of the X register to given address.
LDX #$01
LDA #$aa
STA $a0, X ; store the value of A at memory location $a1
INX ; increment X
STA $a0 , X; store the value of A at memory location $a2

13.Zero page Y

This can only be used with LDX, STX.

when I study this part, it is not easy to understand. it just definition, so I feel that I need practice with sample code. it will help my understanding.

Friday, January 24, 2020

Week 2 Reflection _ What is 6502 architecture




In this week I learned what is the 6502 architecture. During the code I wonder about this. t

The 6502 handles data in its resisters, each of which holds one byte of data. there are a total of three general use and two special purpose resisters.

* purpose resisters


 A (accumulator) - Handles all arithmetic and logic. the real heart of the system.

X and Y - General purpose registers with limited abilities.

S - Stack pointer

P - Processor status. Holds the result of tests and flags.
      The processor status is not directly accessible by any 6502 instruction. Instead, there exist numerous instructions that test the bits of the processor status register. The flags within the register are

       bit ->   7                           0
              +---+---+---+---+---+---+---+---+
              | N | V |   | B | D | I | Z | C |  <-- flag, 0/1 = reset/set

              +---+---+---+---+---+---+---+---+

N - Negative. Set if bit 7 of accumulator is set

V - Overflow. Set if the addition of two like-signed numbers or the subtraction of two unlike-singed numbers produces result greater than +127 or less than -128

B - BRK command. Set if an interrupt caused by a BRK, reset if caused by an external interrupt.

D - Decimal mode. Set if desimal mode active.

- IRQ disable. Set if maskable interrupts are disabled.

Z -Zero. Set if the result if the last operation was zero

C -  Carry. The carry flag is set if the last operation caused an overflow from bit 7 of the result or an underflow from bit 0. This condition is set duriong arithmetic, comparison and during logical shifts.



Saturday, January 18, 2020

Lab2


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.
·         Reset - resets the state of the emulator (see the Memory Map section below).
·         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



< Result >

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



< Result >

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
< Result – 1times>
< Result 2 times>
< Result 3 times>
< 4times repeate>
<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
< Result – 1times>



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 – odd times repeated>

< 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.


< Result >

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 


< Result >

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.