Page 3 of 3

Re: First 6502 assembly program

Posted: Sat Oct 21, 2017 3:41 pm
by KLset
barrym95838 wrote:
Comments and critiques are welcome.
What a great learning experience, thank you. I analyzed it but I need some time to digest! So, like you wrote, the approach is to minimize loop overhead by painting more pixels per iteration. I assume the fastest program would just be a series of STA calls that paints the picture without any branching or jumps.

Here is a solution that does 16 loops, and paints all pixels that are the same color in one iteration:

Code: Select all

main:
    CLD
    LDA  #0
    TAX       ; from left to middle
    LDA  #31
    TAY       ; from right to middle
    LDA  #0   ; starting color is black

loop:
    STA  $200,X  ; paint left
    STA  $220,X
    STA  $240,X
    STA  $260,X
    STA  $280,X
    STA  $2A0,X
    STA  $2C0,X
    STA  $2E0,X
    STA  $300,X
    STA  $320,X
    STA  $340,X
    STA  $360,X
    STA  $380,X
    STA  $3A0,X
    STA  $3C0,X
    STA  $3E0,X
    STA  $400,X
    STA  $420,X
    STA  $440,X
    STA  $460,X
    STA  $480,X
    STA  $4A0,X
    STA  $4C0,X
    STA  $4E0,X
    STA  $500,X
    STA  $520,X
    STA  $540,X
    STA  $560,X
    STA  $580,X
    STA  $5A0,X
    STA  $5C0,X
    STA  $5E0,X
    INX          ; move to the right

    STA  $200,Y  ; paint right
    STA  $220,Y
    STA  $240,Y
    STA  $260,Y
    STA  $280,Y
    STA  $2A0,Y
    STA  $2C0,Y
    STA  $2E0,Y
    STA  $300,Y
    STA  $320,Y
    STA  $340,Y
    STA  $360,Y
    STA  $380,Y
    STA  $3A0,Y
    STA  $3C0,Y
    STA  $3E0,Y
    STA  $400,Y
    STA  $420,Y
    STA  $440,Y
    STA  $460,Y
    STA  $480,Y
    STA  $4A0,Y
    STA  $4C0,Y
    STA  $4E0,Y
    STA  $500,Y
    STA  $520,Y
    STA  $540,Y
    STA  $560,Y
    STA  $580,Y
    STA  $5A0,Y
    STA  $5C0,Y
    STA  $5E0,Y
    DEY          ; move to the left

    ADC #1
    CMP #16
    BCS end
    JMP  loop

end:
    BRK

Re: First 6502 assembly program

Posted: Sat Oct 21, 2017 8:44 pm
by KLset
BigDumbDinosaur wrote:
KLset wrote:
BigDumbDinosaur wrote:
POC V2 generates wait-states by controlling the MPU's RDY input, which is the least problematic method, in my opinion. As Ed noted, changing clock speeds requires some very careful planning to avoid a glitch that will crash the machine.
I found the RDY signal in the datasheet! Let's see if I get this right... So you set the output of the RDY pin to low to indicate that the 6502 is now waiting (WAI mode), and when the device it is interfacing with is ready to go again, that device sends an interrupt to the 6502, for example by sending low to the IRQB pin, to tell the 6502 that "wake up, we're ready to go". Can the 6502 escape from WAI mode by itself somehow, like a timeout?
See Ed's and Garth's responses. I'll reiterate some of what they said.

For the purposes of making the MPU wait on a slow device, RDY is an input, not an output. When RDY is held high, the MPU runs as expected, executing instructions. When RDY is brought low, the MPU stops on the next Ø2 high and remains stopped until RDY is brought high again. While the MPU is stopped in this fashion it maintains the state of the address and data buses, as well as RWB.

In your system, you would have logic that would detect when the current address is that of a device that is slow and requires wait-stating, such as a ROM. That logic would pull RDY low for one or more Ø2 cycles, after which it would return RDY high. If you do a search of the forum you will find additional topics on the use of RDY to generate wait-states. In particular, Jeff Laughton (Dr. Jefyll) posted a circuit that uses a couple of flip-flops and is simple to implement.

On a final note, we have been blithely assuming you are using the 65C02. Unlike the 65C02, the NMOS 6502 doesn't respond to RDY during a write cycle. Hence another method of wait-stating slow peripherals when writing to them has to be devised. It is strongly recommended that you do not use the NMOS parts in a new design, for this reason and others.
I get it now, thanks. I also found the circuit by Dr. Jefyll (viewtopic.php?p=38470#p38470 for future reference).

I have yet to decide on my first project, so at the moment I have no 6502/65C02 nor any other electronics!

Re: First 6502 assembly program

Posted: Sat Oct 21, 2017 9:55 pm
by barrym95838
Yeah, you (KLset) have a firm grasp of the concept involving the trade-off between code size and speed. I noticed that you weren't making use of LDX # and LDY # at the beginning of your latest creation, and that you made the dangerous assumption further down that the carry was clear before the ADC #1 in the first run through the loop.

Happy coding!

Mike B.

Re: First 6502 assembly program

Posted: Sun Oct 22, 2017 6:55 am
by KLset
barrym95838 wrote:
I noticed that you weren't making use of LDX # and LDY # at the beginning of your latest creation, and that you made the dangerous assumption further down that the carry was clear before the ADC #1 in the first run through the loop.
I should have done both of those! Thank you having a look.

Re: First 6502 assembly program

Posted: Sun Oct 22, 2017 8:53 am
by Klaus2m5
Next thing you need to learn is assembler directives. In the Kowalski simulator you can bloat your code automatically:

Code: Select all

    .org $1000
main:
    CLD
    LDX  #0   ; from left to middle
    LDY  #31  ; from right to middle

loop:
    TXA
next_line .set $200
    .rept 32
      STA  next_line,X  ; paint left
      STA  next_line,Y  ; paint right
next_line .set next_line + $20
    .endr
    INX
    DEY
    CPX #16
    BCS end
    JMP  loop

end:
    BRK
Unfortunately every assembler supports a different set and its own style of directives.

The list output from above:
Output.txt
(6.61 KiB) Downloaded 80 times

Re: First 6502 assembly program

Posted: Sun Oct 22, 2017 1:31 pm
by KLset
Klaus2m5 wrote:
Next thing you need to learn is assembler directives. In the Kowalski simulator you can bloat your code automatically: [...]
Very useful! And I see you also made better use of the X register, doubling as both the color and location; nice! I had a look at the ca65 syntax for repetition, and indeed it is not the same as the Kowalski (the equivalent to .rept in ca65 is .repeat, see http://cc65.github.io/doc/ca65.html#ss11.92 for reference).

Re: First 6502 assembly program

Posted: Sun Oct 22, 2017 10:21 pm
by BigDumbDinosaur
KLset wrote:
I had a look at the ca65 syntax for repetition, and indeed it is not the same as the Kowalski (the equivalent to .rept in ca65 is .repeat, see http://cc65.github.io/doc/ca65.html#ss11.92 for reference).
.REPT is an alternative to .REPEAT in the Kowalski assembler.

Re: First 6502 assembly program

Posted: Mon Oct 23, 2017 6:49 am
by KLset
BigDumbDinosaur wrote:
.REPT is an alternative to .REPEAT in the Kowalski assembler.
Sorry! My mistake. It's even in the kowalski_directives.txt that you shared.