Reputation: 996
so I am having an issue with how to work with some addressing in 6502. Basically, I have 3 values that I want to represent the 12 bit address space of a pixel on the monitor of the 6502. I have built the address elements with some simple modding. However, I am not quite sure where to go from here. Let's say I have a goal address of 05ff, the bottom right pixel. What I currently have is the first bit, which would be added to 0200, the second bit, and the third bit, but no idea how to build up a 12 bit quantity from these. Can anyone offer assistance? My teacher kind of left us out to dry other than lectures and the obelisk site, which is still a bit over my head as far as using it to figure out what to do. Does anybody know how I can do this?
Currently, for the address 05ff, what I have is a 3, an f, and an f. I know I can just bit shift to get the second f to move over, but I am not sure how to add the 0200 to the 0300 to produce the quantity 0500, which would then be added to the 00ff.
Upvotes: 0
Views: 462
Reputation: 453
You really do not want to think about manipulating individual bits unless you absolutely have to.
The 6502 is an 8-bit machine so everything has to be manipulated on 8-bit boundaries. If you need less than an "even power of 2" quantity of bits, you should throw away the ones you don't want with a logical AND
. It's more efficient than trying to "save" four bits another way AFAIK.
Example:
;Our first variable. Holds a 12-bit value but we allocate 16 bits
; to it because this is an 8-bit architecture
VAR_A_16_LO EQU $80
VAR_A_16_HI EQU $81
;Our second variable. Same thing as above.
VAR_B_16_LO EQU $82
VAR_B_16_HI EQU $83
;Our variable where we store the result.
VAR_C_16_LO EQU $84
VAR_C_16_HI EQU $85
;Ok, lets add some 12-bit numbers
;But first, we have to make sure the variables have 12-bit
; quantities. This means killing the top 4 bits of the high
; bytes of the variables. If this is known to be the case this
; can be skipped.
LDA VAR_A_16_HI
AND #%00001111 ;This logical AND operation will set the upper
; four bits to 0 and keep the lower four bits.
;Of the high byte only.
STA VAR_A_16_HI
LDA VAR_B_16_HI
AND #%00001111
STA VAR_B_16_HI
;
; Now, we can add them as though they were two 16-bit numbers.
CLC
LDA VAR_A_16_LO
ADC VAR_B_16_LO
STA VAR_C_16_LO
LDA VAR_A_16_HI
ADC VAR_B_16_HI
STA VAR_C_16_HI
;
; And cut off the top 12 bits of the result just in case of
;overflow.
LDA VAR_C_16_HI
AND #%00001111
STA VAR_C_16_HI
Upvotes: 1
Reputation: 100622
To expand on comments already made: the 6502 has only 8-bit registers. It augments these by giving the zero page an elevated status: it's quick to reference and vectored addressing is available through it.
E.g.
; ... calculate high 8 bits of address in A ...
; this assumes that #$00 is stored at address $00
STA $01
; ... calculate low 8 bits of address and move them into Y;
; calculate value to store in A ...
STA ($00), Y
That'll fetch a 16-bit address from the zero page locations $00
and $01
. It'll then add the value in Y
to that address. It'll store A
to the total.
Upvotes: 0