SPO600 - LAB 02
Published: 2022-01-31 22:41:16

Lab 02 - 6502!

This lab goal is to calculate the performance of  a 6502 program.
The program is this one and can be run [HERE](6502 assembler/simulator (cdot.systems)): 
```
	lda #$00	; set a pointer at $40 to point to $0200
	sta $40
	lda #$02
	sta $41

	lda #$07	; colour number

	ldy #$00	; set index to 0

loop:	sta ($40),y	; set pixel at the address (pointer)+Y

	iny		; increment index
	bne loop	; continue until done the page

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

```


In order to calculate how many seconds, the program will take to run, we need to calculate how many cycles each instruction will take and how much time each instruction will take to be completed.

To do that, we have to take a look at [this]( https://www.masswerk.at/6502/6502_instruction_set.html).

 This website provide us the clock values for each instruction, this way we are able to measure the total time that the program will take at 1MHz.

## Lets Calculate Performance

Lets start with our first instruction:
` lda #$00`

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1sdfgc2vy0ronedo1z9h.png)
 

Lda is our first instruction, so now lets take a look at the website to see the cycles values:
 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ftwbtrsskb2ipede7qvq.png)
 


Here we can see that LDA takes 2 cycles, because we are using immediate addressing.

To keep track, lets make a chart with all the values:
 

As we can see in my chart, we are getting the number of cycles, how many times they will happen, the alternative cycles and how many times they will happen and making a sum of the total cycles.

Here we can see the final number of cycles are multiplied by the CPU speed, giving us the time needed to run the program.
 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8ul375i0v2vxv10wwlgk.png)
 

I tried to optimize it and make it run faster but I am finding difficult to work with the pages, my goal was to make for each loop a pixel on each page to become yellow, this way maybe it would be faster but I could not make it work to calculate its speed.

### Experimenting:

The first program will give us this screen: 
 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6h27jk2ts1euuf8lpfep.png)

 
When experimenting with `tya` instruction, we will have this result:

 ![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jn71wnrcn15t83crbotp.png)
 

We can see now that we have stripes with different colors in the screen, `tya` means `transfer Index Y to accumulator` which means that every time the loop happens the accumulator (which it’s the color) will get incremented by one.

But when trying to use `lsa` after `tya` I keep getting this error: 
 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5bf7xksnwn5kbijkwk3r.png)
 
## My thoughts on this Lab
It is really cool to understand how to calculate the cycles and the time that takes for a 6502 program to run, this way we are able to make tests to make it faster.
But I feel like sometimes working with memory addresses can be complicated and confusing, there is many ways to write it and I feel like I need more time to understand it better.
I could not optimize the program the way I wanted to, which made me frustrated but I am decided to come back to this lab in the future and try it again.

Thank you for reading!