Lab 1


Introduction

Starter code:

 	lda #$00	; set a pointer in memory location $40 to point to $0200
 	sta $40		; ... low byte ($00) goes in address $40
 	lda #$02	
 	sta $41		; ... high byte ($02) goes into address $41
 	lda #$07	; colour number
 	ldy #$00	; set index to 0
 loop:	sta ($40),y	; set pixel colour at the address (pointer)+Y
 	iny		; increment index
 	bne loop	; continue until done the page (256 pixels)
 	inc $41		; increment the page
 	ldx $41		; get the current page number
 	cpx #$06	; compare with 6
 	bne loop	; continue until done all pages

Open the 6502 Emulator at http://6502.cdot.systems paste the starter code in the input box.

Click Assemble and Run to see results.

Calculating Performance

    cycles count alt-cycles alt-count total  
  lda #$00 2 1     2  
  sta $40 3 1     3  
  lda #$02 2 1     2  
  sta $41 3 1     3  
  lda #$07 2 1     2  
  ldy #$00 2 1     2  
 loop: sta ($40),y 6 1024     6144  
  iny 2 1024     2048  
  bne loop 3 1020 2 4 3068  
  inc $41 5 4     20  
  ldx $41 4 4     16  
  cpx #$06 2 4     8  
  bne loop 3 3 2 1 11  
               
          total 11329 cycles
               
          clock speed 1 Mhz
          clock length 0.000001 S
            0.001 mS
            1 uS
               
          program time 11329 uS
            11.329 mS
            0.011329 S


-------------------------------------------------------------------------------------------------------------------

Decrease the time (Loop unrolling)

- do more in each loop

  lda #$07 ; colour number
  ldy #$00 ; set index to 0
 loop: sta $0200,y ; set pixel colour at page 2+Y

sta $0300,y ; set pixel colour at page 3+Y

sta $0400,y ; set pixel colour at page 4+Y

sta $0500,y ; set pixel colour at page 5+Y

iny ; increment index
  bne loop ; continue until done the page (256 pixels)









 

Calculating Performance

  loop unrolling            
    cycles count alt-cycles alt-count total  
  lda #$07 2 1     2  
  ldy #$00 2 1     2  
 loop: sta $0200,y 5 256     1280  
  sta $0300,y 5 256     1280  
  sta $0400,y 5 256     1280  
  sta $0500,y 5 256     1280  
  iny 2 256     512  
  bne loop 3 255 2 1 767  
               
          total 6403 cycles
               
          clock speed 1 Mhz
          clock length 0.000001 S
            0.001 mS
            1 uS
               
          program time 6403 uS
            6.403 mS
            0.006403 S

-------------------------------------------------------------------------------------------------------------------

Display with light blue (Set number "$FE")

  lda #$00 ; set a pointer in memory location $40 to point to $0200
  sta $40 ; ... low byte ($00) goes in address $40
  lda #$02
  sta $41 ; ... high byte ($02) goes into address $41
  lda #$FE ; colour number
  ldy #$00 ; set index to 0
 loop: sta ($40),y ; set pixel colour at the address (pointer)+Y
  iny ; increment index
  bne loop ; continue until done the page (256 pixels)
  inc $41 ; increment the page
  ldx $41 ; get the current page number
  cpx #$06 ; compare with 6
  bne loop ; continue until done all pages

-------------------------------------------------------------------------------------------------------------------

Display with a different colour on each page (update data in IDA using ADC)

  lda #$00
; set a pointer in memory location $40 to point to $0200
  sta $40
; ... low byte ($00) goes in address $40
  lda #$02

  sta $41
; ... high byte ($02) goes into address $41
  lda #$FE
; colour number
  ldy #$00
; set index to 0
 loop: sta ($40),y
; set pixel colour at the address (pointer)+Y
  iny
; increment index
  bne loop
; continue until done the page (256 pixels)

adc #$AA
; add on to the accumulator
  inc $41
; increment the page
  ldx $41
; get the current page number
  cpx #$06
; compare with 6
  bne loop
; continue until done all pages

 -------------------------------------------------------------------------------------------------------------------

Experiences

After the first lab, I did assembly the first time. I thought it was very fun even I felt very confused. I wish this was a full term course so I would have more time to consume from the professor. It is a fun to think of the idea to store colours with numbers. It didn't come in to my mind until I hear it from my professor. Thinking of coding in C is already different when I first learned it but at least the syntax is more human. Now doing assembly coding not only have to think of the logic the syntax is on a different level.

 -------------------------------------------------------------------------------------------------------------------

RANDOM

Adding tya

  lda #$00 ; set a pointer in memory location $40 to point to $0200
  sta $40 ; ... low byte ($00) goes in address $40
  lda #$02
  sta $41 ; ... high byte ($02) goes into address $41
  lda #$FE ; colour number
  ldy #$00 ; set index to 0
 loop: tya ; transfer Index Y to Accumulator

sta ($40),y ; set pixel colour at the address (pointer)+Y
  iny ; increment index
  bne loop ; continue until done the page (256 pixels)

adc #$AA
  inc $41 ; increment the page
  ldx $41 ; get the current page number
  cpx #$06 ; compare with 6
  bne loop ; continue until done all pages











output:



-------------------------------------------------------------------------------------------------------------------

     lda #$00    ; set a pointer in memory location $40 to point to $0200
     sta $40        ; ... low byte ($00) goes in address $40
     lda #$02   
     sta $41        ; ... high byte ($02) goes into address $41
     lda #$07    ; colour number
     ldy #$00    ; set index to 0
 loop:    tya    ; transfer Index Y to Accumulator
    tya: lsr    ; shift One Bit Right (Memory or Accumulator)
    sta ($40),y    ; set pixel colour at the address (pointer)+Y
     iny        ; increment index
     bne loop    ; continue until done the page (256 pixels)
     inc $41        ; increment the page
     ldx $41        ; get the current page number
     cpx #$06    ; compare with 6
     bne loop    ; continue until done all pages

output: (the colour line has store in 3 bit width after adding "tya: lsr")

 

 -------------------------------------------------------------------------------------------------------------------

     lda #$00    ; set a pointer in memory location $40 to point to $0200
     sta $40        ; ... low byte ($00) goes in address $40
     lda #$02   
     sta $41        ; ... high byte ($02) goes into address $41
     lda #$07    ; colour number
     ldy #$00    ; set index to 0
 loop:    tya    ; transfer Index Y to Accumulator
    tya: lsr    ; shift One Bit Right (Memory or Accumulator)
    tya: lsr    ; shift One Bit Right (Memory or Accumulator)
    tya: lsr   
; shift One Bit Right (Memory or Accumulator)
    sta ($40),y    ; set pixel colour at the address (pointer)+Y
     iny        ; increment index
     bne loop    ; continue until done the page (256 pixels)
     inc $41        ; increment the page
     ldx $41        ; get the current page number
     cpx #$06    ; compare with 6
     bne loop    ; continue until done all pages

 output: (added few more "tya: lsr")

 

 -------------------------------------------------------------------------------------------------------------------

     lda #$00    ; set a pointer in memory location $40 to point to $0200
     sta $40        ; ... low byte ($00) goes in address $40
     lda #$02   
     sta $41        ; ... high byte ($02) goes into address $41
     lda #$07    ; colour number
     ldy #$00    ; set index to 0
 loop:    tya    ; transfer Index Y to Accumulator
    tya: asl    ; shift Left One Bit (Memory or Accumulator)
    tya: asl    ; shift Left One Bit (Memory or Accumulator)
    sta ($40),y    ; set pixel colour at the address (pointer)+Y
     iny        ; increment index
     bne loop    ; continue until done the page (256 pixels)
     inc $41        ; increment the page
     ldx $41        ; get the current page number
     cpx #$06    ; compare with 6
     bne loop    ; continue until done all pages

output: (use asl)

 

 -------------------------------------------------------------------------------------------------------------------

    lda #$00    ; set a pointer in memory location $40 to point to $0200
     sta $40        ; ... low byte ($00) goes in address $40
     lda #$02   
     sta $41        ; ... high byte ($02) goes into address $41
     lda #$07    ; colour number
     ldy #$00    ; set index to 0
 loop:    sta ($40),y    ; set pixel colour at the address (pointer)+Y
     iny        ; increment index
     iny
     iny
     iny
     iny
     bne loop    ; continue until done the page (256 pixels)
     inc $41        ; increment the page
     ldx $41        ; get the current page number
     cpx #$06    ; compare with 6
     bne loop    ; continue until done all pages

iny - having few iny in this code will make it skip however many you put in it. (y+n every loop) If the iny is even it will fill in all the bits, but if it is odd bigger than 1 it will not have fill all the bits.

-------------------------------------------------------------------------------------------------------------------

LDX #10

LDX $FE
STX $00
STX $01

INC $00
DEC $01

LDX $00
INX
STX $0200
LDX $01
STX $0300
LDX $FE
STX $0400

lda #$00    ; set a pointer in memory location $40 to point to $0200
        
    lda #$00    ; set a pointer in memory location $40 to point to $0200
     sta $40        ; ... low byte ($00) goes in address $40
     lda #$05   
     sta $41        ; ... high byte ($02) goes into address $41
     lda #$07    ; colour number
     ldy #$00    ; set index to 0
    ldx #$00

loop:    sta ($40),y    ; set pixel colour at the address (pointer)+Y
     iny        ; increment index
    ADC #05
    inx
     bne loop    ; continue until done the page (256 pixels)


output:


















Comments

Popular posts from this blog

Project Stage 1

Project Stage 2