Posts

Showing posts from January, 2025

SPO600 Lab2 (Pt.3)-- challenges 3 & 4 and reflection

SPO600 Lab2 (Pt.3)-- challenges 3 & 4 and  reflection Subsequence to part1 and part2 , I am revising a 6502 instruction assembly program which allows a logo to bounce within the screen. Challenges Lets work on Challenges 3 and 4. 1. Permit integer value other than -1 and +1 for the X and Y increments (logo velocity). 2. Permit fractional value for the X and Y increments (eg. +1.5 or -0.75) 3. Perturb the ball's position or velocity  4. Change the graphic image or colour each time it bounces  Problems analysis and solutions Assumption:  1. Only velocity of the bounce direction will be changed. 2. the logo is allowed to stop in case the ball velocity changed to 0 due to the perturbing. (unlikely happening) 3. We do not do perturb to the position since it is just similar to that of velocity. Solution: Simple add a Perturb subroutine so that we can add perturbing when it bounds. code: ; draw-image-subroutine.6502 ; ; This is a routine that can place an arbitrary ;...

SPO600 Lab2 (Pt.2)-- challenges 1 and 2

In Part 1 , I have modified the code of 6502 processor so that an logo can bounce within the screen.  Challenges Now, we have a few challenges. We would like to modified the code so that it can meet the following requirement: 1. Permit integer value other than -1 and +1 for the X and Y increments (logo velocity). 2. Permit fractional value for the X and Y increments (eg. +1.5 or -0.75) 3. Perturb the ball's position or velocity (To be done next blog) 4. Change the graphic image or colour each time it bounces (To be done next blog) Challenge 1.  Problems analysis and solutions Assumption: the velocity cannot be larger than the screen size, i.e. the ball will not bounce more than once within one frame.  Problem of Challenge 1: Since the current algorithm is just checking whether the logo is on the edge to decide whether we should reverse the velocity to perform a bounce. However, it only works when the velocity is 1. If the velocity is 2 and the displacement of the ball fro...

SPO600 Lab2 (Pt.1)

 In this Lab, I was provided with below 6502 processor assembly program. The program is to display a logo on the screen. The logo moves with increment of x and y by 1. When it reach the edge, it will go back to the initial position of top left (x=0, y=0).  Original code is as below (Copyright details in the program beginning comment):- ; ; draw-image-subroutine.6502 ; ; This is a routine that can place an arbitrary ; rectangular image on to the screen at given ; coordinates. ; ; Chris Tyler 2024-09-17 ; Licensed under GPLv2+ ; ; ; The subroutine is below starting at the ; label "DRAW:" ; ; Test code for our subroutine ; Moves an image diagonally across the screen ; Zero-page variables define XPOS $20 define YPOS $21 START: ; Set up the width and height elements of the data structure LDA #$05 STA $12 ; IMAGE WIDTH STA $13 ; IMAGE HEIGHT ; Set initial position X=Y=0 LDA #$00 STA XPOS STA YPOS ; Main loop for diagonal animation MAINLOOP: ; S...

6502 Processor: Stack memory, Stack pointer, program pointer and details of 6502 instructions for jump and rotation

In this blog, I would like to share about program counter(PC), stack , stack pointer(SP) of 6502 processor. I will also share what I learnt regarding the instructions JSR, JMP, RTS, LSR,ASL,ROR, ROL, PHA and PLA while I experienced how they interact with the stack, SP, PC and the register status. Some of the information is from the documentation in the website of mass:werk Program counter (PC) Program counter (PC) is the pointer that keep track of the address of the next instruction to be executed in the memory. The Stack Regarding 6502 processor, the stack is located from $0100 to $01FF. If we store a value into the stack, value is stored starting from $01FF. However, we a 2-byte address is stored, the address closer to the $01FF is the high byte. For example, if we store $0123 to an empty stack. $01FF will be $01 and $01FE will be $23. Stack pointer Stack pointer (SP) is the pointer pointing to the top of stack memory. For 6502 processor, the pointer is pointing to $01FF before the p...

Addition and subtraction with 6502 processor

Image
 I would like to share what I have learnt in my SPO600 course regarding addition and subtraction with 6502 processor. In the processor status register, there is a "carry" flag, which take important role in the addition and subtraction. Each memory is 1 byte in 6502 processor. In the calculation of more than one byte, there could be a value carry over the next byte.  Addition Example: If we add $0001 to $01FF, we first their low bytes, $01 and $FF. Result is $00 and the carry flag will be set. Then, we add the high bytes $00 and $01, then add the carry flag which is 1. The result will be $0200. When we do the addition, other than the STA and LDA, some useful instructions are CLC and ADC.   ADC is the instruction which use to add the memory value to the accumulator. One important point is that if the carry flag is set, the result will add 1 extra. Also, if the result overflow, it will set the carry flag.  For example, if the accumulator's value is $00 and we add #...

SPO600 Lab1 (Pt.5) - Challenges Question 2 Modified solution and reflection

Challenge 2 - Write a program which draws lines around the edge of the display with 6502 processor instruction: A red line across the top A green line across the bottom A blue line across the right side. A purple line across the left side. Modified solution The previous solution was using indirect addressing/ pointer and nested loop. However, since the screen has only 4 pages, it could be more readable and efficient by just using direct addressing and a single loop for the line across the left side/ right side. Here is the revised solution. ; add a purple line accrose left side           ldy #$00 ; set index to 0 leftloop: lda #$04 ;purple           sta $0200,y           sta $0300,y           sta $0400,y           sta $0500,y tya           clc adc #$20 tay bne leftloop ;add a blue line accrose right ...

SPO600 Lab1 (Pt.4) - Challenges

Image
Two challenges regarding 6502 processor instruction will be completed. Challenge 1 - fill in the screen with one color while the middle four pixel will be filled in with another color. Solution: 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       lda #$02        ; second colour number           sta $03ef       ; set pixel colour to the middle four pixels         ...

SPO600 Lab1 (Pt.3) - Experiments

Image
 I am going to try some experiment on the code below: 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   Experiment of adding tya and lsr instructions Firstly, add the "tya" instruction after the loop: label and before the sta ($40),y instruction. We got this output. There are 16 colors shown in the screen. The instruction tya is storing the value in Y register into the Accumulator. Since, the Y-register is storing the offset value, the offset/position value is now stored to the bit-mapped...

SPO600 Lab1 (Pt.2) - Modifying the code

 In this part, I have to revise the code to fulfill the task. task 1:  Change the code to fill the display with light blue instead of yellow. Solution:         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 #$e         ; 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 task 2.  Change the code to fill the display with a different colour on each page The idea of my solution is to store the selected color code in four consecutive memory. Then, use a pointer to reach those color code. Of course, anot...

SPO600 Lab1 (Pt.1) - Calculation of the execution time of a script

Image
In this Lab, we are told to calculate the execution time of a script of 6502 CPU instructions. We assume the clock speed is 1MHz. Meanwhile, when I was studying the 6502 CPU instructions in another  website .  I note that there are different type of addressing. Immediate addressing basically means we use a value instead of an address of memory. "Zeropage" addressing  means that we use a number within $FF, ie the memory is located in the page 0. "Absolute" addressing means that we use a memory location in the form of $00FF. "Indirect" addressing  means we can put the address of a memory, and that memory and the next memory will store the address of the target memory. It works like a pointer. The original Script of Lab 1:           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 ...