6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Wed May 22, 2024 4:00 am

All times are UTC




Post new topic Reply to topic  [ 26 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: 6502 BBC Compiler
PostPosted: Sun Sep 08, 2013 11:04 am 
Offline
User avatar

Joined: Sun Sep 08, 2013 10:24 am
Posts: 740
Location: A missile silo somewhere under southern England
Hi guys

I discovered these forums whilst nosing around the net trying to find help in building my own 6502 (I'm an old school BBC Micro fan :D). I've discovered it's not as easy as it looks lol. However, I'm going to give it a go.

My project involves 3 inital stages:
1) Basic circuit design
2) basic OS design
3) Circuit Build & debug

I've gotten as far as stage 2. For this I've had a couple of options: design & compile the OS on either Beebem (extremely good Emulator), my real BBC Micro model B ... or find a compliler which works on my Windows 7 PC.
There are issues with memory with the first two options so I've opted to go for the for the last option. However, most 6502 compilers (whilst very good!) don't quite give me what I'm after. Because of this I've decided to write my own compiler (not simulator or emulator) in Visual Basic 2010.

I was wondering: If I posted the source code here (when in beta stage - it's alpha atm) would anyone mind having a play and making some (constructive :)) comments? I plan to make the project freely available when finished.

Here's a couple of screen shots of the alpha :

Just loaded:
Image

Running a test program:
Image

As you can see: there are some problems (hence being in alpha). I.e. INX not being allowed to use Implied mode amongst many.
There is also no label/variable implementation yet and I'm dreading coding the 2/3 pass assembler to get variable values correct : it's easy to distinguish between zero page (LDA &E0) and absolute (LDA &0E00) with given address, but when variables are used instead (LDA MYSTORE) then it's less easy to resolve.
Don't worry about how the program looks : I'll make it look a bit nicer when the guts are programmed :).

Any thoughts?

Cheers!

[EDIT] Resolved INX issue - the data file had the wrong mode listed. Fixed INY with same issue.


Top
 Profile  
Reply with quote  
 Post subject: Re: 6502 BBC Compiler
PostPosted: Sun Sep 08, 2013 2:34 pm 
Offline

Joined: Sat Dec 13, 2003 3:37 pm
Posts: 1004
Just what are you looking for in you assembler that you can't find already existing? There seems to be a plethora of Windows assemblers (I can't name names, there are links on site, I don't use them as I don't use Windows).

Writing an assembler, at least a feature filled one, is a pretty major effort. If that's what you're interested in, then knock yourself out. But if you're more interested in the final result, it's probably worth your while to hunt down one you'd like to work with.

As for your zero page issue, the way I did it in mine was that if I knew the value when I first hit the instruction, then I made the appropriate ZP vs absolute decision. If not, then it was absolute and I filled the value in later. Imperfect, but simple and gets the job done.

To clarify, I wrote my assembler because I couldn't find one that ran on the Mac, and that seemed to put out a listing file. My source was the FIG Forth source code, which requires the most basic of assemblers, so I was able to readily get it to that state. It's in Java, portable, and I don't recommend it to anybody.

Today I'm to the point if I wanted to do more serious assembly, I would need to make major restructuring to my assembler, to the point I'd almost start it over in terms of fundamental design. It's one reason I haven't done much with it, as it would yet again be a lot of work.


Top
 Profile  
Reply with quote  
 Post subject: Re: 6502 BBC Compiler
PostPosted: Sun Sep 08, 2013 2:52 pm 
Offline
User avatar

Joined: Sun Sep 08, 2013 10:24 am
Posts: 740
Location: A missile silo somewhere under southern England
The assemblers I found seem to have odd ways of implementing variables and also writing one isn't a huge issue for me. At least this way I can tweak things as I go - and although I'm an amature coder it's something I quite like doing (along with trying my hand at basic electronics lol). I'm thinking of also building in a code clip bank a well which might be nice - but that's for later.
One other (very small) point is that many assmeblers use the $ sign to represent a hex number. I prefer the & sign. I know, I know: it's extremely minor - but it's another reason :D

Zero-page wise, I was thinking along the same lines as you. But another idea is to have a special prefixed variable type which would denote a zero page value. Such as sticking a ~ in front of the variable name, etc.


Top
 Profile  
Reply with quote  
 Post subject: Re: 6502 BBC Compiler
PostPosted: Sun Sep 08, 2013 3:30 pm 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8442
Location: Southern California
I'm in a hurry and can't take time right now but I'll make some quick comments. The variables should always be declared before they are encountered, and then there's no question about wheather they're in ZP or not. We had another discussion on this recently and I can try to find it later to link to if someone doesn't beat me to it. [Edit: It's at viewtopic.php?f=2&t=2507.] When I wrote my tiny Forth assembler, I made it require that the addressing mode be integrated with the mnemonic. That simplified a lot of things and works very well, but it's only for writing Forth primitives and runtimes and assembly subroutines and ISRs, not whole applications, and it's unusual to have more than a few dozen lines of assembly without going back to Forth. Being written in Forth though, it did make assembly macros a piece of cake.

I think it's best to start with two passes and then do additional passes if necessary to get rid of phase errors. Don't limit it to three passes. I had an unusual but valid situation 20 years ago that took many passes (20? I can't rememeber) to get rid of all the phase errors. The extra time it took on a fast PC was not enough to be any problem.

I have a list of 6502 assemblers at http://wilsonminesco.com/links.html#soft, in about the middle of the page. (They're in with links to other 6502 software stuff.)

We had another discussion recently on whether on OS is really necessary also. Again, I can probably find it later and add the link unless someone beats me to it. [Edit: It's at viewtopic.php?f=2&t=2520.] It sounds like you might be mostly beyond this, but I have a short discussion on the necessary steps to a successful project at http://wilsonminesco.com/6502primer/steps.html in the 6502 primer.

_________________
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?


Top
 Profile  
Reply with quote  
 Post subject: Re: 6502 BBC Compiler
PostPosted: Sun Sep 08, 2013 6:31 pm 
Offline
User avatar

Joined: Thu May 28, 2009 9:46 pm
Posts: 8190
Location: Midwestern USA
banedon wrote:
The assemblers I found seem to have odd ways of implementing variables and also writing one isn't a huge issue for me. At least this way I can tweak things as I go - and although I'm an amature coder it's something I quite like doing (along with trying my hand at basic electronics lol). I'm thinking of also building in a code clip bank a well which might be nice - but that's for later.

In my experience, it has been the "amature" (sic) coders who have created the oddities, resulting in assemblers that are in some cases, annoying to use. The standards for the 6502 assembly language were developed by MOS Technology at the time of the MPU's inception and were derived in part from the (older) Motorola 6800 assembly language. Every commercially developed 6502 assembler I have used in the past 35-or-so years has substantially, if not completely, adhered to the MOS Technology standard. The assembler sold by WDC carries on with that standard, with additional features to facilitate writing native mode 65C816 programs.

Garth's suggestion that you structure your assembler to process in two passes and then use additional passes in the event of a phase error is a good one. I would set a limit on the maximum number of passes, only to avoid having the assembler get stuck in a loop if the same phase error repeats. In the overwhelming majority of cases, two passes will resolve all forward references.

Quote:
One other (very small) point is that many assmeblers use the $ sign to represent a hex number. I prefer the & sign. I know, I know: it's extremely minor - but it's another reason :D

Again, $ is used as a hexadecimal radix because it is the MOS Technology standard (also borrowed from 6800 assembly language). The other standard radices are % for binary and @ for octal. Going against the grain won't make your assembler any better, but it will make it harder for experienced programmers to use.

Quote:
Zero-page wise, I was thinking along the same lines as you. But another idea is to have a special prefixed variable type which would denote a zero page value. Such as sticking a ~ in front of the variable name, etc.

Why? If a symbol is declared prior to use then the assembler will be able to correctly resolve it on pass two without need for special notation. Before continuing, please allow me a bit of pedantry.

In assembly language, a declared value, such as BLUE = $03 is referred to as a symbol, not a variable (the word BLUE symbolically represents the value $03). By definition, a symbol cannot be a variable, as the declaration is static. Some advanced assemblers may allow one to declare true variables, whose values can be re-declared at different points in the source code. Specialized coding is required in order for the assembler to recognize a declaration as variable. For example, the assembler in the Kowalski simulator understands that BLUE .SET 3 means BLUE is 3 but could be changed by re-declaration, e.g., BLUE .SET 5. In a literal sense, BLUE is not a true variable, since it is changeable only during assembly time, not run time, as would be the case with a variable in a high level language (e.g., BLUE = BLUE + 1 in BASIC, C, etc.).

In a code line such as CUSMAS *=*+S_CUSMAS, CUSMAS is a label (it marks or labels a memory location); again, CUSMAS is not a variable. Incidentally, S_CUSMAS would be a symbol that was defined earlier in the program, symbolically representing the size of the reserved space referred to by the CUSMAS label.

Declaration of symbols prior to first use is considered to be proper programming style, unless there is a uniquely compelling reason to do otherwise (I've yet to encounter any such reason, and I've written at least a half million lines of 6502 assembly language). In fact, most of the assemblers I have used would abort assembly with an unrecoverable phase error if they encountered CUSMAS *=*+S_CUSMAS prior to declaring S_CUSMAS.

_________________
x86?  We ain't got no x86.  We don't NEED no stinking x86!


Last edited by BigDumbDinosaur on Sun Sep 08, 2013 8:09 pm, edited 1 time in total.

Top
 Profile  
Reply with quote  
 Post subject: Re: 6502 BBC Compiler
PostPosted: Sun Sep 08, 2013 7:33 pm 
Offline
User avatar

Joined: Tue Mar 02, 2004 8:55 am
Posts: 996
Location: Berkshire, UK
My assembler is Open Source and written in Java so it runs on pretty much anything (including Windows 7), supports the 6501/6502/65C02/65SC02/65C816/65C832, has macros/conditional assembly/repeats/includes, can generate absolute or relocatable code, supports structured programming (with no limits on code block sizes), is pretty MOS syntax compliant, and could be modified to allow ampersand in front of hex constants (change the scanToken routine).

I use it with NMAKE to make building apps really simple. I've been using it to create ROM images for my emulator projects.

Details here: http://www.obelisk.demon.co.uk/dev65/index.html
Download here: http://www.obelisk.demon.co.uk/6502/6502.zip
Code here: https://sourceforge.net/projects/dev65/

PM me for a zip with an example project and NMAKE.

_________________
Andrew Jacobs
6502 & PIC Stuff - http://www.obelisk.me.uk/
Cross-Platform 6502/65C02/65816 Macro Assembler - http://www.obelisk.me.uk/dev65/
Open Source Projects - https://github.com/andrew-jacobs


Top
 Profile  
Reply with quote  
 Post subject: Re: 6502 BBC Compiler
PostPosted: Sun Sep 08, 2013 7:46 pm 
Offline
User avatar

Joined: Sun Jun 30, 2013 10:26 pm
Posts: 1930
Location: Sacramento, CA, USA
Well put, BDD.

What happened to your larger font-size? I almost didn't recognize you, because your custom font-size is an integral part of makes your voice "yours". I hope that you just forgot this time, and didn't let BigEd bully you into doing it like everyone else. Celebrate and preserve your uniqueness, octothorpes and all!!! Conformity equals boredom.

Mike


Top
 Profile  
Reply with quote  
 Post subject: Re: 6502 BBC Compiler
PostPosted: Sun Sep 08, 2013 8:08 pm 
Offline
User avatar

Joined: Thu May 28, 2009 9:46 pm
Posts: 8190
Location: Midwestern USA
barrym95838 wrote:
Well put, BDD.

Thanks.

Quote:
What happened to your larger font-size? I almost didn't recognize you, because your custom font-size is an integral part of makes your voice "yours". I hope that you just forgot this time, and didn't let BigEd bully you into doing it like everyone else. Celebrate and preserve your uniqueness, octothorpes and all!!! Conformity equals boredom.

I periodically use a slightly different font size to see if anyone is paying attention. I can assure you that no one has ever succeeded in bullying me, even back in my primary school days. :) The only individual who ever made a serious attempt was on the high school football team—his failure to do so was quite embarrassing for him, as his girl friend was one of the onlookers. :lol:

As for those octothorpes, I keep several around the house as pets. They're quiet, don't eat too much and are never intimidated by the dog.

_________________
x86?  We ain't got no x86.  We don't NEED no stinking x86!


Top
 Profile  
Reply with quote  
 Post subject: Re: 6502 BBC Compiler
PostPosted: Sun Sep 08, 2013 8:15 pm 
Offline

Joined: Sat Oct 20, 2012 8:41 pm
Posts: 87
Location: San Diego
BigDumbDinosaur wrote:
As for those octothorpes, I keep several around the house as pets. They're quiet, don't eat too much and are never intimidated by the dog.

With those eight legs it would scare the dog for sure. :lol:


Top
 Profile  
Reply with quote  
 Post subject: Re: 6502 BBC Compiler
PostPosted: Sun Sep 08, 2013 8:16 pm 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8442
Location: Southern California
BigDumbDinosaur wrote:
Garth's suggestion that you structure your assembler to process in two passes and then use additional passes in the event of a phase error is a good one. I would set a limit on the maximum number of passes, only to avoid having the assembler get stuck in a loop if the same phase error repeats. In the overwhelming majority of cases, two passes will resolve all forward references.

In the one I was using, the progress report on the screen would let you know if it was stuck, and you could abort it at any time if it was taking unreasonably long.

Quote:
Quote:
One other (very small) point is that many assmeblers use the $ sign to represent a hex number. I prefer the & sign. I know, I know: it's extremely minor - but it's another reason :D

Again, $ is used as a hexadecimal radix because it is the MOS Technology standard (also borrowed from 6800 assembly language). The other standard radices are % for binary and @ for octal. Going against the grain won't make your assembler any better, but it will make it harder for experienced programmers to use.

Another way that works with all the assemblers I've used is to put "H" after the number, so you could write for example $1A0 or 1A0H. I suppose we all have our personal peeves. I absolutely detest C's "0x1A0" for example, as "x" in any other context means that either that digit is unknown or it doesn't matter, so it could be $001A0 or 011A0 or $021A0 etc.. And why put a leading 0 on it. I don't put a leading 0 on my age or address or the clock speed of my computer or anything else.

BitWise's assember that he linked to above does seem to be an excellent one, and it's nice that he has built the nestable program structures into it already. I have an article (with accompanying source code for the C32 assembler) on doing them with macros at http://wilsonminesco.com/StructureMacros/index.html for those who want to do it on other assemblers. Anton Treuenfels (teamtempest here on the forum) also has his HXA 6502 assembler with the program-structure capability modeled after the article. These give things like FOR...NEXT, IF...ELSE...END_IF, BEGIN...UNTIL, BEGIN...WHILE...REPEAT, CASE statements, etc. with (in most cases) absolutely no penalty in runtime speed or memory, because they assemble exactly the same code you would write out by hand, just from much more readable source code. Looking at the machine-language output, you wouldn't be able to tell which source code it came from, because it's the same.

However you write your assembler though, I would encourage that you consider macros to be a must-have.

_________________
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?


Top
 Profile  
Reply with quote  
 Post subject: Re: 6502 BBC Compiler
PostPosted: Sun Sep 08, 2013 8:19 pm 
Offline

Joined: Mon Jan 07, 2013 2:42 pm
Posts: 576
Location: Just outside Berlin, Germany
Just out of idle curiosity, is there any text that explains how to write an assembler? Something like the Dragon Book for compilers? Not that I'm planning another project, it would just be interesting to learn how they work internally.


Top
 Profile  
Reply with quote  
 Post subject: Re: 6502 BBC Compiler
PostPosted: Sun Sep 08, 2013 9:00 pm 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8442
Location: Southern California
You mention both a compiler and an assembler. Assuming you're talking about two separate things (which they are), what language do you want to compile?

_________________
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?


Top
 Profile  
Reply with quote  
 Post subject: Re: 6502 BBC Compiler
PostPosted: Sun Sep 08, 2013 9:32 pm 
Offline
User avatar

Joined: Tue Mar 02, 2004 8:55 am
Posts: 996
Location: Berkshire, UK
Assemblers are just simple compilers. You use the same techniques of lexical and syntax analysis, optimisation and code generation. Its just more direct.

_________________
Andrew Jacobs
6502 & PIC Stuff - http://www.obelisk.me.uk/
Cross-Platform 6502/65C02/65816 Macro Assembler - http://www.obelisk.me.uk/dev65/
Open Source Projects - https://github.com/andrew-jacobs


Top
 Profile  
Reply with quote  
 Post subject: Re: 6502 BBC Compiler
PostPosted: Sun Sep 08, 2013 10:51 pm 
Offline

Joined: Sat Dec 13, 2003 3:37 pm
Posts: 1004
banedon wrote:
One other (very small) point is that many assmeblers use the $ sign to represent a hex number. I prefer the & sign. I know, I know: it's extremely minor - but it's another reason :D

May as well switch to the Intel 6502 mnemonics as well.

As they say, "Do what you want, you will anyway." Assembly is "less portable" at the source level than many language, mostly for reason of the pseudo ops and macros.

But a lot of the 6502 stuff is "ok" portable.

If you have no expectation of sharing your source, no reason to follow convention of any kind.


Top
 Profile  
Reply with quote  
 Post subject: Re: 6502 BBC Compiler
PostPosted: Sun Sep 08, 2013 10:54 pm 
Offline

Joined: Sat Dec 13, 2003 3:37 pm
Posts: 1004
BitWise wrote:
My assembler is Open Source and written in Java so it runs on pretty much anything (including Windows 7), supports the 6501/6502/65C02/65SC02/65C816/65C832, has macros/conditional assembly/repeats/includes, can generate absolute or relocatable code, supports structured programming (with no limits on code block sizes), is pretty MOS syntax compliant, and could be modified to allow ampersand in front of hex constants (change the scanToken routine).

But it doesn't produce a listing file, correct?

Like:
Code:
0200           .ORG $0200
0200 A9 00     LDA #$00   ; Clear ACC


style file? or a label dump or cross reference? Does it provide any of those? I didn't see any options.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 26 posts ]  Go to page 1, 2  Next

All times are UTC


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: