Page 9 of 10
Re: From 8bit breadboard to 6502
Posted: Mon Aug 14, 2017 12:44 pm
by nei02
I am trying to display the binary data of a register on the terminal
sta ACIA_DATA
The terminal program thinks it gets an character and try to display it.
But how can I tell the terminal that I just von to print binary data. e.g. "00110011"
Do I need to write a encoding macro?
-Take Byte
-Read each BIT
-if 0 then make "0" if 1 then make "1"
Re: From 8bit breadboard to 6502
Posted: Mon Aug 14, 2017 1:25 pm
by BigEd
That's right, you want to output 8 characters, so you have a little work to do!
Re: From 8bit breadboard to 6502
Posted: Mon Aug 14, 2017 2:41 pm
by mkl0815
I'm using the following code for that in my monitor program. It is written in Ophis Assembler and for the 6502, so optimization for the 65C02 can be done.
k_wchr is the subroutine to output one character.
Code: Select all
;
; print out the 8bit value in A as binary code
; X,Y is preserved, A is destroyed
;
.scope
u_bin8out:
txa ; move X to A
pha ; save (X)
tya ; move Y to A
pha ; save (Y)
ldx #$08 ; counter for 8 bit
_loop: clc ; clear carry flag
asl ; shift byte by one position
bcs _p1
tay ; save A
lda #'0
jmp _cont
_p1: tay ; save A
lda #'1 ; print "1"
_cont: jsr k_wchr
tya ; get A back
dex ; decrement counter
bne _loop
pla
tay ; restore Y
pla ; restore X
tax
rts ; return
.scend
Re: From 8bit breadboard to 6502
Posted: Mon Aug 14, 2017 3:55 pm
by nei02
Thank you for the code example. This helps me to learn assembler.
You shift the byte one bit left (after clearing carry out)
Then you analyse the carry flag. If set than print "1" if not print "0".
I think with 65C02 the code will be shorter because you can push and pull X,y to stack directly.
Cool! Thank you! So this evening I have something to do while my wife is watching Germany next top model.

Re: From 8bit breadboard to 6502
Posted: Mon Aug 14, 2017 3:56 pm
by floobydust
I'm using the following code for that in my monitor program. It is written in Ophis Assembler and for the 6502, so optimization for the 65C02 can be done.
k_wchr is the subroutine to output one character.
Code: Select all
;
; print out the 8bit value in A as binary code
; X,Y is preserved, A is destroyed
;
.scope
u_bin8out:
txa ; move X to A
pha ; save (X)
tya ; move Y to A
pha ; save (Y)
ldx #$08 ; counter for 8 bit
_loop: clc ; clear carry flag
asl ; shift byte by one position
bcs _p1
tay ; save A
lda #'0
jmp _cont
_p1: tay ; save A
lda #'1 ; print "1"
_cont: jsr k_wchr
tya ; get A back
dex ; decrement counter
bne _loop
pla
tay ; restore Y
pla ; restore X
tax
rts ; return
.scend
I believe your code is incomplete. As you specify that the A reg holds the byte value to be printed, your first instruction (TXA) overwrites that value. As it's written, the value in the Y reg would be used. You also don't need the CLC instruction as the carry flag will be set based on the ASL instruction.
Below is the code I use in my monitor to print the status register. CHROUT sends a character and the CROUT send a CR/LF and returns. You can eliminate the LDA PREG and just have the A reg loaded with the value you want to print. You can also replace the JMP CROUT with a RTS if you don't want to print a CR/LF.
Code: Select all
LDA PREG ;Get Status register preset
LDX #$08 ;Get the index count for 8 bits
SREG_LP LDY #$30 ;Get Ascii "zero"
ASL A ;Shift bit into carry
PHA ;Save Current status register value
BCC SRB_ZERO ;If clear, print a "zero"
INY ;Else increment Y reg to Ascii "one"
SRB_ZERO TYA ;Transfer Ascii character to A reg
JSR CHROUT ;Print it
PLA ;Restore current status register value
DEX ;Decrement bit count
BNE SREG_LP ;Branch back until all bits are printed
JMP CROUT ;Send CR/LF and return
Re: From 8bit breadboard to 6502
Posted: Mon Aug 14, 2017 4:34 pm
by mkl0815
I believe your code is incomplete. As you specify that the A reg holds the byte value to be printed, your first instruction (TXA) overwrites that value. As it's written, the value in the Y reg would be used. You also don't need the CLC instruction as the carry flag will be set based on the ASL instruction.
Your are right. I think this happened when I had to backport the following code from 65C02 to 6502 for my MOUSE2Go. because the emulator for the Arduino only supported 6502 opcodes for this time. Seems I never tested this. Thanks for pointing that out.
Here's the 65C02 code:
Code: Select all
;
; print out the 8bit value in A as binary code
; X,Y is preserved, A is destroyed
;
.scope
u_bin8out:
phx ; save X
phy ; save Y
ldx #$08 ; counter for 8 bit
_loop: clc ; clear carry flag
asl ; shift byte by one position
bcs _p1
tay ; save A
lda #'0
bra _cont
_p1: tay ; save A
lda #'1 ; print "1"
_cont: jsr k_wchr
tya ; get A back
dex ; decrement counter
bne _loop
ply ; restore Y
plx ; restore X
rts ; return
.scend
Re: From 8bit breadboard to 6502
Posted: Mon Aug 14, 2017 5:00 pm
by rwiker
Here's a slightly more compact version (posted mainly because I have never posted any 6502 code here before):
Code: Select all
1 *= $300
2
3 prtbin:
4 0300 a208 ldx #8
5 0302 0a loop: asl
6 0303 48 pha
7 0304 a930 lda #$30
8 0306 6900 adc #0
9 0308 20edfd jsr $fded
10 030b 68 pla
11 030c ca dex
12 030d d0f3 bne loop
13 030f 60 rts
14
15 0310 a9e5 testit: lda #$e5
16 0312 200003 jsr prtbin
Argument in A, overwrites A and X. The "jsr fded" should make it clear what computer (or emulator) I tested with.
Re: From 8bit breadboard to 6502
Posted: Mon Aug 14, 2017 5:05 pm
by BigEd
If the printing subroutine preserves Y, then I'd probably use TAY/TYA instead of PHA/PLA - it's quicker, but also for me just a bit of a preference.
Re: From 8bit breadboard to 6502
Posted: Mon Aug 14, 2017 6:05 pm
by floobydust
Here's a slightly more compact version (posted mainly because I have never posted any 6502 code here before):
Code: Select all
1 *= $300
2
3 prtbin:
4 0300 a208 ldx #8
5 0302 0a loop: asl
6 0303 48 pha
7 0304 a930 lda #$30
8 0306 6900 adc #0
9 0308 20edfd jsr $fded
10 030b 68 pla
11 030c ca dex
12 030d d0f3 bne loop
13 030f 60 rts
14
15 0310 a9e5 testit: lda #$e5
16 0312 200003 jsr prtbin
Argument in A, overwrites A and X. The "jsr fded" should make it clear what computer (or emulator) I tested with.
Very nice, two bytes shorter than my current routine, I like it.
Re: From 8bit breadboard to 6502
Posted: Tue Aug 15, 2017 3:27 pm
by nei02
Hi,
managed to print binary numbers and managed to use the VIA Interrupt and an ISR.
Thanks you for the Interrupt primer. helped a lot.
I like the picture of the not so important boy you should wait at the backdoor because he always forgets to unlock the door.

Re: From 8bit breadboard to 6502
Posted: Sat Aug 26, 2017 7:42 am
by nei02
I am really happy! For me it is a big step!
I managed to program a little monitor which allows me to copy assembled code via terminal to RAM and execute.
For all of you this is certainly an "old hat". But for me it is just wonderful!

Re: From 8bit breadboard to 6502
Posted: Mon Sep 04, 2017 2:42 pm
by czuhars
Believe me, you're not the only one enjoying and benefiting from these discussions. I'm working on my own single-board machine and the issues/questions you've brought up are the same as I have had. These guys are phenomenal in the time and patience it takes to help a newcomer get their projects working. We're very fortunate to have such a group.
I'm also a big fan of Ben Eater's videos, and have learned much from them.
My goal is an 8-bit machine with bank-switchable RAM and movable zero page, ala the Apple ///. But I have to get a simple machine going first.
I too am using an Arduino (Mega) in my development. It's a wonderful device to test and see what the computer is doing. The Mega is nice in that it has enough i/o to dedicate pins to the address and data lines without bit-shifting complicating things for a newbie. Fewer points of failure, etc. I'll also use it for my EEPROM programming.
Good luck to you!
Chris Z
Re: From 8bit breadboard to 6502
Posted: Mon Sep 04, 2017 6:26 pm
by nei02
Yes I learned really a lot! And got a lot of help in this forum!
What I have done since my last post:
- I took the WozMonitor from the Apple I (found it in www) and adapted the code to my environment.
OPHIS and Symon
- Wrote a Monitorprogramm by myself to load, store, read and run. (of course the code is not optimised with regard to speed and storage. I am no professionell assembler programmer)
I also would like to recommend the book: 6502 Assembly Language Subroutines from Leventhal and Saville.
I found a link to download the pdf here in this forum! Very, very good!
I dont know what to do next. Maybe implement an inline assembler like in the mouse2Go project. Bit don't know where to get this?:-o
Re: From 8bit breadboard to 6502
Posted: Mon Sep 04, 2017 7:19 pm
by GARTHWILSON
I also would like to recommend the book: 6502 Assembly Language Subroutines from Leventhal and Saville.
Be sure you go further though. That book only uses the NMOS instruction set which is quite limiting
compared to that of the CMOS 65c02 that you have. Use the resources on this site and in the sites of the members.
Re: From 8bit breadboard to 6502
Posted: Mon Sep 04, 2017 7:55 pm
by nei02
Thanks! Yes I will!
I like the Symon Emulator. And it seems that it only supports the NMOS Opcodes.
