flat assembler
Message board for the users of flat assembler.

Index > Non-x86 architectures > FASM for Zilog Z80 CPU

Goto page 1, 2, 3  Next
Author
Thread Post new topic Reply to topic
shutdownall



Joined: 02 Apr 2010
Posts: 518
Location: Munich
shutdownall
I think it's better to open a new thread to this.
I want to make a fasm version for Zilog's Z80 CPU as I found nothing comfortable in the internet and work quite a time with FASM which is a very good development tool.

I just found this documentation:
http://board.flatassembler.net/topic.php?t=208

Is this the right and maybe an easy way to implement a new instruction set ?
Post 21 Nov 2011, 21:06
View user's profile Send private message Send e-mail Reply with quote
DOS386



Joined: 08 Dec 2006
Posts: 1901
DOS386
You might want to remove all the existing x86 instructions and formats other than binary.
Post 23 Nov 2011, 14:28
View user's profile Send private message Reply with quote
shutdownall



Joined: 02 Apr 2010
Posts: 518
Location: Munich
shutdownall
Today I started.
Implemented first instruction which was a little bit hard or lets say tricky.

LD r,r´

So in Z80 we have register names (=symbols) with only one character (A,B,C,D,E,H,I,L,R), which was not supported by FASM.
But now it works as I extended this structure.

Changes:
include statements in FASM.INC

include '..\..\Z80.inc' instead of x86_64.inc
include '..\..\tablesz80.inc' instead of tables.inc
include '..\..\parserz80.inc' instead of parser.inc

parser.inc need only one line changed for using symbols with one character, x86_64 inc is extended by new instructions but used main part of existing functions, tables is nearly completely different as it contains the instruction set in main.

So I think it could be done maybe quite fast.
Wink
Post 23 Nov 2011, 23:04
View user's profile Send private message Send e-mail Reply with quote
shutdownall



Joined: 02 Apr 2010
Posts: 518
Location: Munich
shutdownall
Now I have one problem to solve.
Maybe somebody can help me (hopefully Very Happy ).

FASM uses [] for reg/mem or mem/reg addressing.
In the Z80 manual the writing for such addressing is with ().

Example:

Code:
mov ax,[bx] ; for intel
ld A,(HL) ; for Z80
    


This looks maybe easy to sovle with a macro but could be more complex.

Code:
mov ax,[SI+5] ; for intel
ld A,(IX+5) ; for Z80
    


There maybe exist complex calculations in some programs (I don't know how often such things are used ...) using () as well.

Code:
mov ax,[SI+((address1-address2)/2-(10*opsize))] ; for intel
ld A,(IX+((address1-address2)/2-(10*opsize))) ; for Z80
    


I don't know if the last line is a valid syntax for Z80 assembly but for new developments this could be used. So I cannot replace all "(" with "[" and all ")" with all "]". It maybe has to be done in the context. This affects FASM in general as there is another concept of using () and [].

Any ideas ? Maybe a macro specialist here Very Happy
So only the outer () should be replaced with [], beginning after the ",".
Or before beginning of "," for mem/reg addressing but this could be another macro.
First looking for ",(" and second for ")," (but then backwards). Shocked
Post 24 Nov 2011, 17:06
View user's profile Send private message Send e-mail Reply with quote
shoorick



Joined: 25 Feb 2005
Posts: 1608
Location: Ukraine
shoorick
shutdownall wrote:
Code:
ld A,(IX+((address1-address2)/2-(10*opsize))) ; for Z80    

I don't know if the last line is a valid syntax for Z80 assembly but for new developments this could be used.

except properest way - to write a new assembler from scratch, you may choose from that easiest things:
1.look source of fasm for using of "[]" and "()" and swap them, then you might use this form for calculations:
Code:
ld A,(IX+[[address1-address2]/2-[10*opsize]]) ; for Z80    

2.if such calculation was not valid - you may left fasm logic as is and use "[]" for indirect access, but then you will have to replace all "()" with "[]", when you wish to use already existing sources. it may appear not hard, but annoying, or even lead to troubles when "()" used in comments or as character values.

_________________
UNICODE forever!
Post 25 Nov 2011, 09:27
View user's profile Send private message Visit poster's website Reply with quote
shutdownall



Joined: 02 Apr 2010
Posts: 518
Location: Munich
shutdownall
Maybe I have to do something in the "parse line" function.
It's only for lines with instructions.

Here are some examples:

LD A,(HL) => must be translated to LD A,[HL]
LD A,(address-offset)*2 => must not be translated
LD A,address*(sizebig-sizesmall) => must not be translated

LD A,(address-offset) is not allowed since () is obsolete in this case, maybe could left untouched but maybe could cause an error. I don't know how to deep I am going in the source to do that. Probably I don't wanted but it's getting more and more. Rolling Eyes

I thought about a solution like replace ( with [ and ) with ] under following conditions:

( must appear directly after the "," and ) must be the last char of the line except of comments.

Or for the LD (HL),A vice versa.

Would this be okay or did I forget some other cases not covered by my examples ?
Post 25 Nov 2011, 15:33
View user's profile Send private message Send e-mail Reply with quote
shutdownall



Joined: 02 Apr 2010
Posts: 518
Location: Munich
shutdownall
So I skipped the problem with () and [] for the first step and integrated now all possible "movement" instructions (ld/push/pop). Has some more with EX (exchange) but Zilog does not list them under this section movements. Razz

By doing it I found first error in the original Zilog Z80 document (from 2005).
http://www.zilog.com/docs/z80/um0080.pdf

The opcodes for POP IY are wrong. It's FD E1 and not as written DD FD (which are both prefixes for IX IY register set instead of HL. A possibly copy and paste error I think. Maybe I find more. But I did not proove it found just some irregularities in coding of this instruction.

I hope to finish the first version with all commands before starting holiday in decembre. So I think you all can be sure to have a FASM version for Z80 - with some luck this year, if I could not spend too much time maybe in january.
Post 26 Nov 2011, 18:45
View user's profile Send private message Send e-mail Reply with quote
typedef



Joined: 25 Jul 2010
Posts: 2913
Location: 0x77760000
typedef
maybe you can just make a parser that will parse from Intel (after compiling in FASM) to z80 instead of making a "feel" for the Z80
Post 26 Nov 2011, 20:30
View user's profile Send private message Reply with quote
shutdownall



Joined: 02 Apr 2010
Posts: 518
Location: Munich
shutdownall
typedef wrote:
maybe you can just make a parser that will parse from Intel (after compiling in FASM) to z80 instead of making a "feel" for the Z80


I don't know what you mean. Shocked

There are some really big differences between x86 and Z80. registerset is different, instructionset is different and syntax is different. If you want to avoid errors in written code like unallowed register combinations, unallowed offsets and many more it does not make sense to compile Z80 code with FASM for x86 instruction set and after change the coding.
Post 26 Nov 2011, 21:59
View user's profile Send private message Send e-mail Reply with quote
shutdownall



Joined: 02 Apr 2010
Posts: 518
Location: Munich
shutdownall
By doing it I found second error in the original Zilog Z80 document (from 2005).
http://www.zilog.com/docs/z80/um0080.pdf

The opcodes for XOR s are wrong. These are just copied from OR s without change. Have an older manual from Zilog which was scanned only as PDF, the newer document as plain text PDF is new written by somebody I think.
Post 27 Nov 2011, 22:35
View user's profile Send private message Send e-mail Reply with quote
shutdownall



Joined: 02 Apr 2010
Posts: 518
Location: Munich
shutdownall
status:

finished now additional all
=> exchange instructions
=> block instructions
=> 8-bit arithmetic instructions
=> general purpose arithmetic & cpu controls

I think about 50% of commands implemented now. Very Happy
Post 28 Nov 2011, 00:12
View user's profile Send private message Send e-mail Reply with quote
shutdownall



Joined: 02 Apr 2010
Posts: 518
Location: Munich
shutdownall
added:
=> 16-bit arithmetic instructions
=> all shift and rotate instructions

to be done:
=> bit instructions
=> jump instructions
=> call instructions
=> in/out instructions

I think implementation now finished about 70%.
Post 29 Nov 2011, 23:41
View user's profile Send private message Send e-mail Reply with quote
shutdownall



Joined: 02 Apr 2010
Posts: 518
Location: Munich
shutdownall
Due to holiday this will be continued in about 4 weeks.
I tried coding BIT 0,A instruction which is a bit tricky and now not have too much time to go deeper into it.
Z80 version should be available beginning of january, with luck maybe this year but not sure. Razz
Post 01 Dec 2011, 10:57
View user's profile Send private message Send e-mail Reply with quote
flaith



Joined: 07 Feb 2005
Posts: 122
Location: $300:20 58 FC 60 N 300G => Vietnam
flaith
Thanks for your work shutdownall Smile

_________________
Je suis sur de 'rien', mais je ne suis pas sur du 'tout'.
Post 02 Dec 2011, 12:48
View user's profile Send private message Visit poster's website Reply with quote
shutdownall



Joined: 02 Apr 2010
Posts: 518
Location: Munich
shutdownall
So now I continue working on the Z80 version.
Bit instructions coded today.

So now still open:

=> jump instructions
=> call instructions
=> in/out instructions

Should be implemented soon, have enough time this week. Wink
Post 03 Jan 2012, 00:08
View user's profile Send private message Send e-mail Reply with quote
shutdownall



Joined: 02 Apr 2010
Posts: 518
Location: Munich
shutdownall
So now the complete instructionset of Z80 is fully implemented. Very Happy
The only open point is to interprete the ZILOG syntax with

LD A,(HL)
LD A,(IX+4)

which FASM expect as

LD A,[HL]
LD A,[IX+4]

But calculations with () have to be untouched like

LD A,(varx+5)*varsize

for example (which is an immediate instruction with a variable).

So the first step is done, if I changed the above described problem I can post the first version here. So now it's not far away. Razz
Post 05 Jan 2012, 22:37
View user's profile Send private message Send e-mail Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3502
Location: Bulgaria
JohnFound
Hm, why not to leave it as it is. All FASM users are accustomed to the square brackets meaning indirect addressing.
The round brackets was in use in the ancients times, so IMHO it is time for small modernization of the syntax.
Post 06 Jan 2012, 07:53
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
shutdownall



Joined: 02 Apr 2010
Posts: 518
Location: Munich
shutdownall
I don't think so because an assembler has to be compatible to the preferred syntax in manuals AND (more important) has to translate existing programs without any change. Anyway, Z80 is complete different to x86 and you maybe need a manual first to see which registers are allowed for which operation (with ZILOG syntax, of course).

And the use of [] for memory addressing from Intels x86 is not a standard in general - just a defined (proposed) syntax from Intel. But more horrible is the AT&T syntax in my eyes. I think any experienced Z80 programmer would not use [] instead (). And any FASM x86 programmer has to learn Z80 first to develop good and efficient programs.

MAYBE (but only maybe) I can find a solution to use both syntax elements, () and [] for same purpose. Anyway if you are programming Z80 you have to think like Z80 in Z80 structures and registers. And maybe it is a good idea to keep the syntax different to avoid instructions not available. Razz
Post 06 Jan 2012, 13:27
View user's profile Send private message Send e-mail Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4242
Location: 2018
edfed
even if pure z80 programming is using (), now, we are in 2012.
very nice project, i want to do the same for any CPU i can programm (not a lot for the moment), and franckly, even if the prehistoric code is in old syntax, i'd prefer to modify everything by hand with new syntax than to code any line in old syntax.

keep it up. sega genesis have a z80 to control the sound, and a 68k as main cpu, it can be nice to code for this one too Wink
Post 06 Jan 2012, 20:48
View user's profile Send private message Visit poster's website Reply with quote
shutdownall



Joined: 02 Apr 2010
Posts: 518
Location: Munich
shutdownall
As pointed out, Z80 syntax is not prehistoric and [] are not a standard.
Every developer of new CPU types defines the syntax to be used.
Keep a look at AT&T syntax, by the way I don't know if FASM support the AT&T syntax.
I never used it and it is the first and only CPU with two total different coding syntax.
I think people learned from that problem.
The best way is to have only ONE syntax.
And the syntax belongs to the CPU - not to the assembly coder.

Anyway I try to fulfill both ways use of either () or [].

And to point this out:
This is a separate version of FASM which ONLY supports Z80 instruction set, no mix possible between Z80 and x86 code (!). I even can not see any reason for support of both CPU's in one FASM.EXE. I think this could be done with mixing code but this would be extra work to melt both structures in one program. And it's really more complicate to do syntax check.

I will also write a manual for adapting new CPU types and how to handle them in FASM. As Tomasz had never time to write that down (only one very old document with some few hints).
Post 06 Jan 2012, 23:21
View user's profile Send private message Send e-mail Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page 1, 2, 3  Next

< Last Thread | Next Thread >
Forum Rules:
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 vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.