I don't recall seeing a complete schematic for your unit, but I suspect the clock stretching thing wouldn't be all that difficult to set up. As I earlier said, I'm working on something for my POC V1.2 unit to test someone else's clock stretching theory.
Starting with my first SBC Project, Plans and Confusions
- BigDumbDinosaur
- Posts: 9428
- Joined: 28 May 2009
- Location: Midwestern USA (JB Pritzker’s dystopia)
- Contact:
Re: Starting with my first SBC Project, Plans and Confusions
Proxy wrote:
not sure how i would do "clock stretching" with my current PCB, since the Clock sadly doesn't connect to one of the GAL's outputs.
I don't recall seeing a complete schematic for your unit, but I suspect the clock stretching thing wouldn't be all that difficult to set up. As I earlier said, I'm working on something for my POC V1.2 unit to test someone else's clock stretching theory.
Quote:
anyways, after fixing that i ran right into the next problem, when the CPU writes data into the FT240X i only receive 0xFF on the Terminal. checking with my logic probe the WE on the FT240X gets set to low while PH2 is High. and clearly it is writing something, otherwise it wouldn't send anything.
x86? We ain't got no x86. We don't NEED no stinking x86!
Re: Starting with my first SBC Project, Plans and Confusions
Proxy wrote:
so i got the FT240X to work... basically i just turned the WR output from the GAL from Active-Low to Active-High and it works now.
except now i seem to have a logic error in my program and i cannot fiquire out why.
just to be sure i understand this correctly (i come from the Z80, so the 6502 is quite strange with some things for me)
the Overflow (V) flag i supposed to be set when some arithmetic instruction... well overflows.
so like adding 0x01 to the Accumulator when it contains 0xFF.
and when it doesn't overflow it gets cleared, right
except now i seem to have a logic error in my program and i cannot fiquire out why.
just to be sure i understand this correctly (i come from the Z80, so the 6502 is quite strange with some things for me)
the Overflow (V) flag i supposed to be set when some arithmetic instruction... well overflows.
so like adding 0x01 to the Accumulator when it contains 0xFF.
and when it doesn't overflow it gets cleared, right
For now, I'd suggest forgetting and ignoring the overflow flag. It's not quite what you think.
What you want to think about is the carry flag.
Add 1 to $FF and you get $00 with the carry set. (the 9th bit).
Then you can branch carry set or clear (BCS, BCC) without needing an explicit test.
And note about the explicit test - if you load A with 0 (or it, or any register or memory becomes zero) then the Z flag will be automatically set. That's the key thing to clever 6502 code - the condition flags are automatic based on the last result - Zero or Carry are the 2 you'll use most. So simply:
Code: Select all
loop:
LDA $1234,X
BEQ done
.. print A
INX
BRA loop
done:
-Gordon
--
Gordon Henderson.
See my Ruby 6502 and 65816 SBC projects here: https://projects.drogon.net/ruby/
Gordon Henderson.
See my Ruby 6502 and 65816 SBC projects here: https://projects.drogon.net/ruby/
- BigDumbDinosaur
- Posts: 9428
- Joined: 28 May 2009
- Location: Midwestern USA (JB Pritzker’s dystopia)
- Contact:
Re: Starting with my first SBC Project, Plans and Confusions
Proxy wrote:
just to be sure i understand this correctly (i come from the Z80, so the 6502 is quite strange with some things for me)
the Overflow (V) flag i supposed to be set when some arithmetic instruction... well overflows.
the Overflow (V) flag i supposed to be set when some arithmetic instruction... well overflows.
Code: Select all
clv ;clear overflow
clc ;clear carry
lda #$7f ;decimal 127
adc #$01 ;decimal 1
bvs signovr ;branch if sign overflow <—— this branch will be taken
;
bvc signok ;branch if sign not overflowed
;
bcs carry ;etc...Quote:
then i don't get why this code doesn't work, all i'm doing is using X as an index to a String, but once i increment X at the end (from 0 to 1) the V flag gets set for some reason. i know that because right afterwards is a conditional relative jump that i supposed to jump back to the start of the loop when the overflow is not set. and when it is set it completely restarts the function (ie all registers back to 0 and SP back to 0xFF)
I grabbed a snippet from my Arduino Logic Analyzer and split it into the sperate instructions with some comments.
Instructions are labeled with ">>>>" infront of them
I grabbed a snippet from my Arduino Logic Analyzer and split it into the sperate instructions with some comments.
Instructions are labeled with ">>>>" infront of them
Code: Select all
This part just loads 0xFF into the SP.
>>>>LDX 0xFF
A: 0xE001; D: 0xA2; RD; HIGH
A: 0xE002; D: 0xFF; RD; HIGH- Hexadecimal values in standards-based 6502 assembly language are notated as $nn or $nnnn. 0xNN is C notation. Even x86 assembly language doesn't use that—it uses NNh to indicate hex.
- Immediate register loads are indicated to the assembler with # preceding the value to be loaded. Hence your first instruction would be written as LDX #$FF.
Quote:
Code: Select all
>>>>TXS
A: 0xE003; D: 0x9A; RD; HIGH...
Compare A to 0x00 to see if the String is over
>>>>CMP 0x00
A: 0xE00B; D: 0xC9; RD; HIGH
A: 0xE00C; D: 0x00; RD; HIGHQuote:
Code: Select all
Increment X (the pointer into the String)
>>>>INX
A: 0xE01D; D: 0xE8; RD; HIGH
Check if X overflowed (overflew?), if it didn't go back to the "Main Loop"
>>>>BVC 0xE8 (0xE008)
A: 0xE01E; D: 0x50; RD; HIGH
A: 0xE01E; D: 0x50; RD; HIGH
A: 0xE01F; D: 0xE8; RD; HIGH
x86? We ain't got no x86. We don't NEED no stinking x86!
Re: Starting with my first SBC Project, Plans and Confusions
BigDumbDinosaur wrote:
Specifically, overflow occurs when signed arithmetic results in a "carry" from bit 6 to bit 7. In signed arithmetic, integers range from $00 to $7F in the positive direction, and $FF to $80 in the negative direction. Here's some contrived code to illustrate the principle:
Code: Select all
clv ;clear overflow
clc ;clear carry
lda #$7f ;decimal 127
adc #$01 ;decimal 1
bvs signovr ;branch if sign overflow <—— this branch will be taken
;
bvc signok ;branch if sign not overflowed
;
bcs carry ;etc...BigDumbDinosaur wrote:
Two things:
- Hexadecimal values in standards-based 6502 assembly language are notated as $nn or $nnnn. 0xNN is C notation. Even x86 assembly language doesn't use that—it uses NNh to indicate hex.
- Immediate register loads are indicated to the assembler with # preceding the value to be loaded. Hence your first instruction would be written as LDX #$FF.
so i'm also gonna keep using that because i like how clean it is.
"0x" for Hexadecimal, "0b" for Binary, and nothing for Decimal.
it's just so nice without the use of special characters that would require me to press SHIFT+something.
BigDumbDinosaur wrote:
Incrementing or decrementing a register has no effect on the V flag. The only arithmetic instructions that affect it are ADC and SBC.
the program now works after changing the V flag to the C flag.
finally i got some code working. and it runs perfectly at 1MHz and even seems to work at 10MHz without a RDY Circuit! which is good news
and my poor terminal program can barely keep up with all the "Hello World!"s filling it.
I'm very thankful for the help i got here with my project. even if a lot of issues were caused by my own stupidity, but hey mistakes happen. and this will definitely not be the last project i'll post about on here.
now it's time to get a bit more into the programming, gonna do the basic functions first. stuff like reading a string from the FT240X, writing a string to it, etc.
that usually gets me used to a CPU/Syntax as i already know exactly how those functions are supposed to work. luckly it isn't my first time using assembly.
- BigDumbDinosaur
- Posts: 9428
- Joined: 28 May 2009
- Location: Midwestern USA (JB Pritzker’s dystopia)
- Contact:
Re: Starting with my first SBC Project, Plans and Confusions
Proxy wrote:
now it's time to get a bit more into the programming, gonna do the basic functions first. stuff like reading a string from the FT240X, writing a string to it, etc. that usually gets me used to a CPU/Syntax as i already know exactly how those functions are supposed to work. luckly it isn't my first time using assembly.
x86? We ain't got no x86. We don't NEED no stinking x86!
Re: Starting with my first SBC Project, Plans and Confusions
BigDumbDinosaur wrote:
Proxy wrote:
now it's time to get a bit more into the programming, gonna do the basic functions first. stuff like reading a string from the FT240X, writing a string to it, etc. that usually gets me used to a CPU/Syntax as i already know exactly how those functions are supposed to work. luckly it isn't my first time using assembly.
I've actually designed a custom CPU based on the 65C02, but i only looked at the instructions and basic functions, stuff like interrupts, and flags i did completely from scratch... so i never learned how those work in the original hardware.
luckly the NESdev.com provides a complete PDF of the book so i don't need to buy anything.
anyways i'll still want to do a second revision of the board to make some things better.
like having a power switch so i don't have to unplug the board to turn it off.
having a seperate Reset for the USB chip and the rest of the system. (so that resetting the system doesn't reset the USB connection, which otherwise gets annoying as it has to reconnect to the PC)
maybe replacing the GALs with a CPLD to save some space
I need to think about what else could be done. but those are some ideas i had.
Re: Starting with my first SBC Project, Plans and Confusions
Regarding the flags: handling them well is, in my experience, core to how good your assembly code will be. I find obelisk's 6502 instruction set reference quite useful since it gives the details of exactly what flags are modified by each instruction, helping me to choose carefully what instructions I'm using based on what they set or don't set. But for quicker reference, I've also created a table of flags listing all the instructions that affect each one.
Well, certainly bikeshedding here, but I find using one character rather than two to be more clean. Either way, it's two keystrokes, one in the number row and one in the bottom row, so there doesn't seem to be any difference there.
Proxy wrote:
so i'm also gonna keep using that because i like how clean it is.
"0x" for Hexadecimal, "0b" for Binary, and nothing for Decimal.
it's just so nice without the use of special characters that would require me to press SHIFT+something.
"0x" for Hexadecimal, "0b" for Binary, and nothing for Decimal.
it's just so nice without the use of special characters that would require me to press SHIFT+something.
Last edited by cjs on Thu Mar 12, 2020 11:41 pm, edited 1 time in total.
Curt J. Sampson - github.com/0cjs
Re: Starting with my first SBC Project, Plans and Confusions
INC, INX, INY, DEC, DEX, DEY only affect the Z and N flags. This can be frustrating if you want to be clever with your loop termination conditions, but once you know the idioms it can actually be useful, since it allows you to preserve the Carry flag between iterations of a loop. In general, N and Z are affected by any instruction which changes A, X, Y or performs an operation directly on memory.
The only instructions which affect the V flag are: ADC, SBC, PLP, RTI, CLV - and the /SO signal input (there's no SEV instruction). It's the least-used flag on the 6502, only really useful for error handling during signed arithmetic, and when was the last time you saw a program which did that? (The C language doesn't make it easy.) Hence the secondary use as an external hardware input; /SO is normally tied high, hence unused.
Carry is used not only by ADC and SBC, but also by CMP and the shift and rotate instructions. It is preserved by loads, increments, decrements, and bitwise logical operations (other than shifts and rotates). Some routines also use it as an error flag on exit, as it's easy to set and test explicitly.
The only instructions which affect the V flag are: ADC, SBC, PLP, RTI, CLV - and the /SO signal input (there's no SEV instruction). It's the least-used flag on the 6502, only really useful for error handling during signed arithmetic, and when was the last time you saw a program which did that? (The C language doesn't make it easy.) Hence the secondary use as an external hardware input; /SO is normally tied high, hence unused.
Carry is used not only by ADC and SBC, but also by CMP and the shift and rotate instructions. It is preserved by loads, increments, decrements, and bitwise logical operations (other than shifts and rotates). Some routines also use it as an error flag on exit, as it's easy to set and test explicitly.
Re: Starting with my first SBC Project, Plans and Confusions
cjs wrote:
Regarding the flags: handling them well is, in my experience, core to how good your assembly code will be. I find obelisk's 6502 instruction set reference quite useful since it gives the details of exactly what flags are modified by each instruction, helping me to choose carefully what instructions I'm using based on what they set or don't set. But for quicker reference, I've also created a listing all the instructions that affect each one.
Well, certainly bikeshedding here, but I find using one character rather than two to be more clean. Either way, it's two keystrokes, one in the number row and one in the bottom row, so there doesn't seem to be any difference there.
Proxy wrote:
so i'm also gonna keep using that because i like how clean it is.
"0x" for Hexadecimal, "0b" for Binary, and nothing for Decimal.
it's just so nice without the use of special characters that would require me to press SHIFT+something.
"0x" for Hexadecimal, "0b" for Binary, and nothing for Decimal.
it's just so nice without the use of special characters that would require me to press SHIFT+something.
For those not aware, while $ may well be the convention in the MOS world, but &num is the convention in Acorn/BBC Micro world.
-Gordon
--
Gordon Henderson.
See my Ruby 6502 and 65816 SBC projects here: https://projects.drogon.net/ruby/
Gordon Henderson.
See my Ruby 6502 and 65816 SBC projects here: https://projects.drogon.net/ruby/
Re: Starting with my first SBC Project, Plans and Confusions
Agreed! Of course you have to use the conventions which your assembler uses, but we shouldn't find it any barrier that one person illustrates with &FA and another with $FA.
Re: Starting with my first SBC Project, Plans and Confusions
ok because noone asked i'm still gonan talk about the assembler i use, just because i feel like it deserves some praise. 
I never really found a good 6502/65C02 assembler. the 6502.org's tool page isn't really that great for finding one, especially since it doesn't seem very updated (some sites on there don't exist anymore for example).
basically all assemblers listed are either mainly for some specific platform, cost money/require a key, run on very old Operating systems (ie DOS), or are bound to some specific emulator (which can be useful in cases)
so i opted for an Assembler i already knew... because i used for custom CPUs.
CustomASM
It supports global and local labels, bit slicing, constants, and probably more i can't think of right now. it has a full feature list on the Github.
it's a Universal Assembler, which means you need to define the instruction set and names of instructions yourself.
and that is exactly what i did for the 6502 and 65C02.
though technically i did 2 seperate CPU files for both the 6502 and 65C02.
CS (Classic Syntax), which is something like this:
ZS (Z80 Syntax), which is something like this: (and probably easier to remember than 3 letter acronyms, atleast for me)
another great thing about it is that it doesn't come with some predefined IDE, you can use whatever Text Editor you want.
though i would REALLY recommend Notepad++, it's such an overall amazing text editor.
it even supports custom highlight, which is perfect for an Assembler.
example:
No Custom Highlighting: Custom Highlighting (made it myself to fit my dark background): overall CustomASM nice to use and probably a much better option than a lot of the existing ones, atleast for starters i think.
you literally just drag your ASM files onto the EXE and it assembles them, or you use the command line if you want some debugging info.
I never really found a good 6502/65C02 assembler. the 6502.org's tool page isn't really that great for finding one, especially since it doesn't seem very updated (some sites on there don't exist anymore for example).
basically all assemblers listed are either mainly for some specific platform, cost money/require a key, run on very old Operating systems (ie DOS), or are bound to some specific emulator (which can be useful in cases)
so i opted for an Assembler i already knew... because i used for custom CPUs.
CustomASM
It supports global and local labels, bit slicing, constants, and probably more i can't think of right now. it has a full feature list on the Github.
it's a Universal Assembler, which means you need to define the instruction set and names of instructions yourself.
and that is exactly what i did for the 6502 and 65C02.
though technically i did 2 seperate CPU files for both the 6502 and 65C02.
CS (Classic Syntax), which is something like this:
Code: Select all
LOOP:
LDA STRING,X
BEQ EXIT
STA OUT
INX
BCC LOOP
EXIT:
STP
Code: Select all
LOOP:
LD A, (STRING, X)
JR Z EXIT
LD (OUT), A
INC X
JR NC LOOP
EXIT:
HALT
though i would REALLY recommend Notepad++, it's such an overall amazing text editor.
it even supports custom highlight, which is perfect for an Assembler.
example:
No Custom Highlighting: Custom Highlighting (made it myself to fit my dark background): overall CustomASM nice to use and probably a much better option than a lot of the existing ones, atleast for starters i think.
you literally just drag your ASM files onto the EXE and it assembles them, or you use the command line if you want some debugging info.
Re: Starting with my first SBC Project, Plans and Confusions
Proxy wrote:
ok because noone asked i'm still gonan talk about the assembler i use, just because i feel like it deserves some praise. 
I never really found a good 6502/65C02 assembler. the 6502.org's tool page isn't really that great for finding one, especially since it doesn't seem very updated (some sites on there don't exist anymore for example).
basically all assemblers listed are either mainly for some specific platform, cost money/require a key, run on very old Operating systems (ie DOS), or are bound to some specific emulator (which can be useful in cases)
so i opted for an Assembler i already knew... because i used for custom CPUs.
CustomASM
It supports global and local labels, bit slicing, constants, and probably more i can't think of right now. it has a full feature list on the Github.
it's a Universal Assembler, which means you need to define the instruction set and names of instructions yourself.
and that is exactly what i did for the 6502 and 65C02.
I never really found a good 6502/65C02 assembler. the 6502.org's tool page isn't really that great for finding one, especially since it doesn't seem very updated (some sites on there don't exist anymore for example).
basically all assemblers listed are either mainly for some specific platform, cost money/require a key, run on very old Operating systems (ie DOS), or are bound to some specific emulator (which can be useful in cases)
so i opted for an Assembler i already knew... because i used for custom CPUs.
CustomASM
It supports global and local labels, bit slicing, constants, and probably more i can't think of right now. it has a full feature list on the Github.
it's a Universal Assembler, which means you need to define the instruction set and names of instructions yourself.
and that is exactly what i did for the 6502 and 65C02.
My ever lasting to-do list includes writing an assembler for my Ruby 816 platform - one that runs natively on it... But until then I'm using ca65 from the cc65 suite as it ticks all those boxes (free, and I use Linux and a text editor and Makefiles although I think there are ports to MS Win, etc.)
Cheers,
-Gordon
--
Gordon Henderson.
See my Ruby 6502 and 65816 SBC projects here: https://projects.drogon.net/ruby/
Gordon Henderson.
See my Ruby 6502 and 65816 SBC projects here: https://projects.drogon.net/ruby/
- GARTHWILSON
- Forum Moderator
- Posts: 8775
- Joined: 30 Aug 2002
- Location: Southern California
- Contact:
Re: Starting with my first SBC Project, Plans and Confusions
I had not heard of CustomASM either. If you find dead links on the non-forum part of this site, let us know so we can fix them. Sometimes they just need to be corrected; and other times, depending on the type of page, it might be appropriate to change the link to point to an archived page on archive.org. There's a lot preserved there. Even after Ed and others pointed it out, it took me a while to start thinking rather automatically to look there when a page or site goes down.
I also have a list of 65xx assemblers on my own site's link page at http://wilsonminesco.com/links.html#assem . I try to keep it up to date; but with probably one or two thousand links on my site, it's easy to miss when one goes down or needs fixing; so let me know if you find a dead link there too. I'm constantly updating my site, and my last edit on that page was today, and the last previous was two days ago.
I'm using C32 (linked there), under DOS, but it runs under Windows too. It's not free (it's $99), but it's an excellent macro assembler that works for dozens of processors, and they give you the information to extend it for a processor of your own design if you wish. I have written program-structure macros for it to give it HLL-like structures, and written it up at http://wilsonminesco.com/StructureMacros/, and there are more-extended examples in the last 40% of my page on simple methods for multitasking without a multitasking OS, at http://wilsonminesco.com/multitask/index.html#cycexec . This has really improved my productivity in, and enjoyment of, assembly language, and reduced bugs and improved maintainability. Our own Andrew Jacobs (forum name BitWise) has written an assembler he calls As65 which has similar program-structure capability built in and looks excellent, and our own Anton Treuenfels also has his HXA 6502 assembler where he has adopted my structures. (Disclaimer: I don't have any experience with either of these.)
I think most assemblers allow you to use the text editor of your choice. The big cheese in professional programmers' text editors today seems to be UltraEdit. I think MultiEdit [1] is right up there too. (I use the latter.) They allow you to do all the things you'd expect from a professional programmers' text editor.
[1] Edit, 8/9/25: I just found out, from the Wikipedia article that MultiEdit is defunct, due to the heart-attack death of the man who really made it go, and that even the website has been gone since Aug 2022.
I also have a list of 65xx assemblers on my own site's link page at http://wilsonminesco.com/links.html#assem . I try to keep it up to date; but with probably one or two thousand links on my site, it's easy to miss when one goes down or needs fixing; so let me know if you find a dead link there too. I'm constantly updating my site, and my last edit on that page was today, and the last previous was two days ago.
I'm using C32 (linked there), under DOS, but it runs under Windows too. It's not free (it's $99), but it's an excellent macro assembler that works for dozens of processors, and they give you the information to extend it for a processor of your own design if you wish. I have written program-structure macros for it to give it HLL-like structures, and written it up at http://wilsonminesco.com/StructureMacros/, and there are more-extended examples in the last 40% of my page on simple methods for multitasking without a multitasking OS, at http://wilsonminesco.com/multitask/index.html#cycexec . This has really improved my productivity in, and enjoyment of, assembly language, and reduced bugs and improved maintainability. Our own Andrew Jacobs (forum name BitWise) has written an assembler he calls As65 which has similar program-structure capability built in and looks excellent, and our own Anton Treuenfels also has his HXA 6502 assembler where he has adopted my structures. (Disclaimer: I don't have any experience with either of these.)
I think most assemblers allow you to use the text editor of your choice. The big cheese in professional programmers' text editors today seems to be UltraEdit. I think MultiEdit [1] is right up there too. (I use the latter.) They allow you to do all the things you'd expect from a professional programmers' text editor.
[1] Edit, 8/9/25: I just found out, from the Wikipedia article that MultiEdit is defunct, due to the heart-attack death of the man who really made it go, and that even the website has been gone since Aug 2022.
http://WilsonMinesCo.com/ lots of 6502 resources
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?
Re: Starting with my first SBC Project, Plans and Confusions
Alright, i think i'm done with version 2 of my Board.
what i did:
* Fixes the Reset button and removed the connection to the USB Chip. there is not really a reason to ever reset the FT240X and it's annoying that resetting the System also resets it. (it's annoying because that disconnects it from the PC for a short time)
* changed the placement of the USB connector and the size of the PCB, now it's slightly smaller than before.
* Added a power switch, just a standard 3 pin Slide switch. It's connected in such a way that disabling the power connects all Vcc connections to GND. (with the obvious exception of the USB Port and the USB Chip). it's to prevent the power pins of all ICs and such from being floating while not switched on. not sure if needed.
This is how i hooked up the power switch. I don't know if the switch can technically be on both positions (i doubt it) so i don't know if i should add a resistor between it and GND. Other than that i don't think i changed much. i tried to keep as much of the original design as i could because i know it works.
lastly, i found out Kicad has a Raytracing feature for the 3D viewer... took a minute to caluclate but doesn't look that bad. even got reflections on the board. This has nothing to do with the design, i just found it interesting.
let me know if there are any other changes i should probably do before ordering the boards.
what i did:
* Fixes the Reset button and removed the connection to the USB Chip. there is not really a reason to ever reset the FT240X and it's annoying that resetting the System also resets it. (it's annoying because that disconnects it from the PC for a short time)
* changed the placement of the USB connector and the size of the PCB, now it's slightly smaller than before.
* Added a power switch, just a standard 3 pin Slide switch. It's connected in such a way that disabling the power connects all Vcc connections to GND. (with the obvious exception of the USB Port and the USB Chip). it's to prevent the power pins of all ICs and such from being floating while not switched on. not sure if needed.
This is how i hooked up the power switch. I don't know if the switch can technically be on both positions (i doubt it) so i don't know if i should add a resistor between it and GND. Other than that i don't think i changed much. i tried to keep as much of the original design as i could because i know it works.
lastly, i found out Kicad has a Raytracing feature for the 3D viewer... took a minute to caluclate but doesn't look that bad. even got reflections on the board. This has nothing to do with the design, i just found it interesting.
let me know if there are any other changes i should probably do before ordering the boards.
Re: Starting with my first SBC Project, Plans and Confusions
You should be very careful about separating the power planes of the USB interface and the rest of the board. If the USB chip drives a high logic level into any logic whose power is not only removed but grounded, you can expect that signal to short through the protection diodes of whatever logic is connected to it. Does the USB chip interpret its selection signals as active-low?