lab1
This blog is about the lab1 in spo600, in week 2, the lecture have introduced me the basic of assembly language in term of using the assembly language to perform basic task like fill the page with color, optimazion of the code to improve the performance, changing the code for different color, implement different colors for different pages and get random color for thhe pixels in the pages. Now I would like to perform my work for the lab1
I have calculate the total time for the code that lab 1 has provided for me by checking the instruction set and excel, also, by looking at the bytes for each instruction, it is easy to find out the total memory for the whole program. For the initialization part which is from lda #$00 to ldy #$00, the total cycle would be 14 cycles, in the main loop, the loop has been executed 256 time for each 4 pages. So for the bne loop, if it is in the same page, the cycle will be 3 and in the other page, it will be 2. Hence the total main loop will be (6 + 2 + 3) * 255 = 2805 and for the last iteration, it will be (6 + 2 + 2) = 10. This is the total cycles for one page. Hence for the total 4 pages, it will become (2805 + 10) * 4 = 11260. Lastly for the page iteration, which is the instruction from inc $41 to bne loop, the total cycles will become 39 cycles and for the last final iteration (the bne loop cycle will become 2), the cycle is 12 The total cycle is 14+11260+39+12=11325 cycles. The total memory will be 25 bytes. With assuming a 1 MHz clock speed, the total time is 11325 uS. Next I will talk about how to optimize the code for better performance.
cpx #$06
comparison. After optimization, the code will become lda #$00
sta $40
lda #$02
sta $41
lda #$07
ldx #$04
ldy #$00
page_loop:
sta ($40),y
iny
bne page_loop
inc $41
dex
bne page_loop
And the total cycle will be 11,264 + 37 + 14 = 11,315 cycles, which is smaller than the original cycles.
Lastly, I would like to talk about the modifying code to achieve the requirement of changing color, having different color for each page, and set the random color for each pixels. In order to change the color, we need to change the lda to #$e. This will change the color to light blue. For the second requiremtn, I need to first set the initial color for the first page, and then in the iteration of next page, I will increment the lda for different color in this page. By doing this, it will has different color for each page. The code will be :
lda #$00
sta $40 ; Low byte ($00) goes in address $40
lda #$02
sta $41 ; High byte ($02) goes into address $41
lda #$8 ; Start with color $e0 (you can change this starting color)
sta $42 ; Store the current color in $42
ldy #$00 ; Set index to 0
loop:
lda $42 ; Load the current color
sta ($40),y ; Set pixel color at the address (pointer)+Y
iny ; Increment index
bne loop ; Continue until done the page (256 pixels)
inc $41 ; Increment the page
inc $42 ; Increment the color for the next page
ldx $41 ; Get the current page number
cpx #$06 ; Compare with 6
bne loop ; Continue until done all pages.
For the last midification of code, I have psudo-random number generator. First I need to store the seed at 42 by the line of code sta $42, and then in each loop I will need to use the code lda $42 asl asl clc adc $42 adc #$11 sta $42 to first get the seed from memory and then shift the value and then add a constant value to the seed and then store the new seed back to $42. Then use the code sta ($40),y to set the pixel color to random color. The complete code would be
lda #$00
sta $40 ; Low byte ($00) goes in address $40
lda #$02
sta $41 ; High byte ($02) goes into address $41
; Initialize random number generator
lda #$FF
sta $42 ; Store the seed in $42
ldy #$00 ; Set index to 0
loop:
; Generate next pseudo-random number
lda $42
asl
asl
clc
adc $42
adc #$11
sta $42 ; Store new random number back in $42
sta ($40),y ; Set pixel color at the address (pointer)+Y to the random color
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
lda #$00
sta $40
lda #$02
sta $41
lda #$07
ldx #$04
ldy #$00
page_loop:
sta ($40),y
iny
bne page_loop
inc $41
dex
bne page_loop
These are the work I have done for lab 1.
评论
发表评论