LAB 1

Introduction & Set up

In this lab we will be using a 6502 emulator, we will delve into the fundamentals of its assembly language. This includes topics such as bitmap coding, code modification, execution time and cycle calculations, addressing modes, and instruction sets.


Bitmap Code

The following code fills the emulator's bitmapped display with the colour yellow:

   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

Display Result:


Calculating Performance

Assuming a clock speed of 1 MHz, the following Excel picture will display the cycle, count, alternative cycle ,and alternative count for each instruction.Also execution time, and total memory usage.

Total cycles = (cycles * count) + (alt-cycles * alt-count)
Eexcution time = total cycles * cycle time

Memory usage( the following Excel picture will display the memory usage for each instruction and total usage for all the instructions):


Optimization(decrase the execution of the program)
We can use direct addressing to process four pages simultaneously in each iteration, which helps save execution time by reducing the number of iterations required to update multiple memory pages:
updated code:

  lda #$07         ; colour number
  ldy #$00         ; set index to 0
 loop:
        sta $0200,y      ; store the colour value at address $0200 + y
        sta $0300,y      ; store the colour value at address $0300 + y
        sta $0400,y      ; store the colour value at address $0400 + y
        sta $0500,y      ; store the colour value at address $0500 + y
  iny         ; increment index
  bne loop          ; branch back if y is not 0
 

After updated instructions,we saved 4916 cycles and 0.004916s execution time.

Modifying the Code

1.Change with light blue($6:Blue): 

Code:
     lda #$06 ; colour number
     ldy #$00 ; set index to 0
 loop:
           sta $0200,y ; store the colour value at address $0200 + y
           sta $0300,y  ; store the colour value at address $0300 + y
           sta $0400,y  ; store the colour value at address $0400 + y
           sta $0500,y  ; store the colour value at address $0500 + y
     iny ; increment index
     bne loop ;branch back if y is not 0
Result:
2.Change the code to fill the display with a different colour on each page:
Code:

    ldy #$00 ;  set index to 0
 loop:
  lda #$02 set colour number
          sta $0200,y ; store the colour value at address $0200 + y
  lda #$03 ; set colour number
          sta $0300,y    ; store the colour value at address $0300 + y
  lda #$04 set colour number
          sta $0400,y    ; store the colour value at address $0400 + y
          lda #$05 ; set colour number
          sta $0500,y    ; store the colour value at address $0500 + y
    iny  ; increment index
    bne loop ; branch back if y is not 0
 
Result:

3.Make each pixel a random colour:

Code:
    ldy #$00 ;  set index to 0
 loop:
  lda $fe         ; set random colour number
          sta $0200,y ; store the colour value at address $0200 + y
  lda $fe         ; set random colour number
          sta $0300,y    ; store the colour value at address $0300 + y
  lda $fe         ; set random colour number
          sta $0400,y    ; store the colour value at address $0400 + y
          lda $fe          ; set random  colour number
          sta $0500,y    ; store the colour value at address $0500 + y
    iny  ; increment index
    bne loop ; branch back if y is not 0
Result:

Experiments

1.Add this instruction after the loop: label and before the sta ($40),y instruction: tya:

result:
The result has changed because the "TYA" instruction transfers the index value (Y) to the memory, instead of the colour value. As a result, each memory location stores the index value rather than the colour value we defined.


2.Add this instruction after the tya: lsr:

result:"LSR" instruction shifts the bits one position to the right, dividing the value by 2 in the accumulator,therefore the value of the accumulator is devided by 2 before storing in the memory and discarding any fractional parts.Therefore, after incrementing the value of the Y register, the value in the accumulator is divided by two and then displayed on the screen.

3.Testing lsr instruction

Two lsr instruction: After incrementing the value of the Y register, the value in the accumulator is divided by two twice and then stored at memory address ($40) + Y.
Three lsr instruction: After incrementing the value of the Y register, the value in the accumulator is divided by two three times and then stored at memory address ($40) + Y
Four lsr instruction:After incrementing the value of the Y register, the value in the accumulator is divided by two four times and then stored at memory address ($40) + Y
4.Testing asl instruction
Two asl instruction:"ASL" instruction shifts the bits one position to the left, multiplies the value by 2 in the accumulator,therefore the value of the accumulator is multiplied by 2 before storing in the memory.Therefore, after incrementing the value of the Y register, the value in the accumulator is mlitipled by two and then stored at memory address ($40) + Y.



Three asl instruction:After incrementing the value of the Y register, the value in the accumulator is multipled by two three times and stored at memory address ($40) + Y.

Four asl instruction:After incrementing the value of the Y register, the value in the accumulator is mulitipled by two four times  and stored at memory address ($40) + Y.



5.Test with one to five consecutive iny instructions

With one iny,The Y register is incremented once in the loop.Each time we run the sta ($40), y instruction, we are storing the color value at the address calculated by $40 + Y.
With two iny instruction:Y register is incremented twice per loop iteration.By the time sta ($40), y executes, the Y register has moved two steps ahead. This means we are skipping every alternate pixel.The Y register overflows just before the BNE instruction on the first run and goes to the next page.

With three iny instruction:Y register is incremented four times per loop iteration.By the time sta ($40), y executes, the Y register has moved three steps ahead. This means we are skipping three alternate pixel at a time.However, when the Y register overflows during the first run, it is incremented again by the INY instruction before reaching the BNE instruction, causing the loop to repeat,and filled all the pixel with yellow colour.
With four iny instruction:Y register is incremented four times per loop iteration.By the time sta ($40), y executes, the Y register has moved four steps ahead. This means we are skipping four alternate pixel at a time.The Y register overflows just before the BNE instruction on the first run and goes to the next page.


With five iny instruction:Y register is incremented five times per loop iteration.By the time sta ($40), y executes, the Y register has moved five steps ahead. This means we are skipping five alternate pixel at a time.However, when the Y register overflows during the first run, it is incremented again by the INY instruction before reaching the BNE instruction, causing the loop to repeat,and filled all the pixel with yellow colour.

Challenges

Set Red colour on the top:
                     LDA  #$02          
                     LDY  #$00          
RedLoop:    STA  $0200,y      
                    INY                      
                    CPY  #$20           
                    BNE  RedLoop    

Set Green colour on the bottom:
                      LDA  #$05        
                      LDY  #$00        
GreenLoop:  STA  $05E0,y    
                      INY                   
                      CPY  #$20           
                     BNE  GreenLoop 

Set Purple colour on the left side(I couldn't find a better, more concise approach to achieve this):
LDA #$04
STA $0200
STA $0220
STA $0240
STA $0260
STA $0280
STA $02A0
STA $02C0
STA $02E0
STA $0300
STA $0320
STA $0340
STA $0360
STA $0380
STA $03A0
STA $03C0
STA $03E0
STA $0400
STA $0420
STA $0440
STA $0460
STA $0480
STA $04A0
STA $04C0
STA $04E0
STA $0500
STA $0520
STA $0540
STA $0560
STA $0580
STA $05A0
STA $05C0
STA $05E0

Set Blue colour on the right side(I couldn't find a better, more concise approach to achieve this):
LDA #$06
STA $021F
STA $023F
STA $025F
STA $027F
STA $029F
STA $02BF
STA $02DF
STA $031F
STA $033F
STA $035F
STA $037F
STA $039F
STA $03BF
STA $03DF
STA $03FF
STA $041F
STA $043F
STA $045F
STA $047F
STA $049F
STA $04BF
STA $04DF
STA $04FF
STA $051F
STA $053F
STA $055F
STA $057F
STA $059F
STA $05BF
STA $05DF
STA $05FF

Result:

Conclusion:

In this lab, I learned how to write simple programs using 6502 assembly language. I worked with bitmap code to control the colours on the screen and made the program run faster by optimizing the code. I tested different instructions to see how they change the display, like making colours appear in different areas or changing how the program stores information. By improving the code, I reduced the time it takes to run the program and made it more efficient. This lab showed how small changes in the code can make a big difference in performance and the final result.









 





Comments

Popular posts from this blog

PROJECT STAGE2

PROJECT STAGE1

LAB 4