Difference between revisions of "Z80"

From SizeCoding
Jump to: navigation, search
(Added link to sine generator + some header adjustments)
 
(59 intermediate revisions by 7 users not shown)
Line 1: Line 1:
 
== Introduction ==
 
== Introduction ==
 
Wanting to start sizecoding on a Z80 platform in this day and age can be tough.  
 
Wanting to start sizecoding on a Z80 platform in this day and age can be tough.  
 +
So here is a bit of help to get you started:
  
So here is a bit of help to get you started:
+
== Z80 Based Plaforms ==
 +
*'''[[ZX Spectrum]]''' - ZX Spectrum Sizecoding information
 +
*'''[[Amstrad CPC]]''' - Amstrad CPC Sizecoding information
  
=== Z80 for X86 programmers  ===
+
== Registers ==
Z80 can be seen as the little 8bit brother of X86 chipsets, with many similarities.
+
The Z80 can be seen as the little 8-bit brother of X86 chipsets, with many similarities.
 
If you are coming from a X86 background, this might help you get a bit more grip on the Z80.
 
If you are coming from a X86 background, this might help you get a bit more grip on the Z80.
 
=== Registers ===
 
 
These are the register pairs of the Z80, as seen from a X86 programmers perspective.
 
These are the register pairs of the Z80, as seen from a X86 programmers perspective.
  
AF = AL + Flags
+
* AF = AL + Flags
BC = Can be seen as CX (B=CH,C=CL), often used for loops  
+
* HL = Can be seen as BX (H=BH,L=BL) or SI in a (HL) setting, like BX also used for addressing.
DE = Can be seen as DX (D=DH,E=DL) or DI in a (DE) setting
+
* BC = Can be seen as CX (B=CH,C=CL), often used for loops  
HL = Can be seen as BC (H=BH,L=BL) or SI in a (SI) setting, like BX also used for adressing.
+
* DE = Can be seen as DX (D=DH,E=DL) or DI in a (DE) setting
IX = 16 bit Index Register X, can also be accessed with IXH,IXL  
+
* IX = 16 bit Index Register X, can also be accessed with IXH,IXL  
IX = 16 bit Index Register Y, can also be accessed with IYH,IYL  
+
* IY = 16 bit Index Register Y, can also be accessed with IYH,IYL
  
=== Instructions ===
+
For each of the main registers there also exists a shadow register. These cannot be accessed directly, but must be swapped in and out with the main register set. The shadow registers are usually denoted by the ' symbol. They can be swapped with the following commands:
Here is a rough translation for some of the Z80 instructions:
+
* EX AF,AF' = Swaps AF with AF'
 +
* EXX = Swaps BC, DE and HL with BC', DE' and HL'
 +
There are no shadow registers for the index registers.
  
[code]
+
Note: For a lot of operations, you can only use the A(8bit) and HL(16bit) registers.
BIT = TEST
+
The Sjasmplus assembler has extra syntax and fake-instructions support which may produce unexpected results when source contains other than official Zilog syntax (but the parser can be configured to work in more relaxed way allowing more variations in syntax).
CP = CMP
 
DJNZ = LOOP (decreass B and checks not zero)
 
EX = XCHG
 
EXE = Exchange all registers with Shadow registers, can be used a bit like PUSHA/POPA
 
HALT = HLT
 
JP = JMP
 
JR = JMP NEAR (Jump Relative)
 
LD = MOV
 
LDI = LODSB
 
LDIR = REP MOVSB
 
  
== ZX Spectrum  ==
+
=== Instructions crossreference to X86 ===
The ZX Spectrum consists of a Z80A @ 3.5 MHz CPU with either 16k, 48k or 128K of RAM.
+
Here is a rough translation for some of the Z80 instructions:
 
 
=== Setting up ===
 
Setting up your development platform for the ZX Spectrum is quite easy, first get the following tools:
 
 
 
* Assembler: SJASMPLUS -This assembler has nice macros for creating Binaries and SNA snapshot files out of the box. You can download it at https://sourceforge.net/projects/sjasmplus/
 
* Emulator(s): I Found FUSE, UnrealSpeccy and EightyOne to work best for my usecase. Most emulators can read TAP, SNA and TRD files out of the box.
 
 
 
=== Video diplay ===
 
Video display on the ZX Spectrum is mostly CPU based with little hardware features. No hardware sprites, no specific text or video modes, only a 256x192 byte screenbuffer with 1bit pixeldata located at $4000 in memory.
 
It is ordened a bit strange in 3 sections of 256x64 pixels, then character rows, then subrows.
 
 
 
ScreenPosition = (page<<11) + (character row<<8) + (subrow<<5)
 
 
 
 
 
=== Adding Color ===
 
The ZX Spectrum has a 32x24 colormap located at $5800 where you can write color information for each 8x8 tile.
 
It has has 8 colors (INK and PAPER) with 2 brightness settings that can be set like this.
 
 
 
color = brightness(64) | (PAPER<<3) | INK
 
 
 
You can set the border color to any of the 8 colors with:
 
 
 
<syntaxhighlight lang="z80">
 
out (254),color
 
</syntaxhighlight>
 
  
=== Getting something on screen ===
+
* BIT = TEST
To be added soon.
+
* CP = CMP  (although the Z80 has many other handy compare functionality)
 +
* DJNZ = LOOP (decreases B and checks not zero)
 +
* EXE = Exchange all registers with Shadow registers, can be used a bit like PUSHA/POPA
 +
* EX = XCHG
 +
* HALT = HLT
 +
* JP = JMP
 +
* JR = JMP NEAR (Jump Relative)
 +
* LD = MOV
 +
* LDI = MOVSB  (tmp=(HL),(DE)=tmp, DE++, HL++)
 +
* LDIR = REP MOVSB  (tmp=(HL),(DE)=tmp, DE++, HL++, BC--)
  
 +
== Learning Z80 Assembler ==
 +
There are many Z80 tutorials available online, but one i found very simple and clear is at this 1996 styled webpage ;-) 
  
=== Sound ===
+
* http://www.z80.info/lesson1.htm
The original Spectrum has only a 1 bit sound capability (BEEP) through its internal speaker.
+
* http://www.z80.info/lesson2.htm
Later models included the AY-3-8910 Soundchip which provides 3 channels of PSG sound.
+
* http://www.z80.info/lesson3.htm
 +
* http://www.z80.info/lesson4.htm
 +
* http://www.z80.info/lesson5.htm
  
=== Make some noise - Beeper ===
+
There is no proper index-page for this, which is why i linked all the lessons above, but you can continue to the next lesson by clicking at the next lesson at the bottom of the page.
To be added soon.
 
  
=== Make some noise - AY ===
+
== Sinus Generator Routines ==
To be added soon.
+
* https://github.com/neonz80/sine (example code is for the ZX Spectrum but works on any Z80 platform)
  
=== Additional Resources ===
+
== Other Resources ==
I found resources on ZX Spectrum sizecoding to be sparse.
+
Also, here is a compact 'cheat sheet' with some basics for various Z80 systems:
* Blogpost on ZX Spectrum coding from a X86 coder's perspectove on superogue's sizecdoing blog (soon)
+
https://www.chibiakumas.com/z80/CheatSheet.pdf
* 128byte/256 byte zx spectrum productions by goblinish on pouet.net https://www.pouet.net/groups.php?which=11696&order=platform&reverse=1
 
* 128byte/256 byte zx spectrum productions by gasman on pouet.net https://www.pouet.net/user.php?who=2260&show=credits
 

Latest revision as of 16:24, 21 November 2024

Introduction

Wanting to start sizecoding on a Z80 platform in this day and age can be tough. So here is a bit of help to get you started:

Z80 Based Plaforms

Registers

The Z80 can be seen as the little 8-bit brother of X86 chipsets, with many similarities. If you are coming from a X86 background, this might help you get a bit more grip on the Z80. These are the register pairs of the Z80, as seen from a X86 programmers perspective.

  • AF = AL + Flags
  • HL = Can be seen as BX (H=BH,L=BL) or SI in a (HL) setting, like BX also used for addressing.
  • BC = Can be seen as CX (B=CH,C=CL), often used for loops
  • DE = Can be seen as DX (D=DH,E=DL) or DI in a (DE) setting
  • IX = 16 bit Index Register X, can also be accessed with IXH,IXL
  • IY = 16 bit Index Register Y, can also be accessed with IYH,IYL

For each of the main registers there also exists a shadow register. These cannot be accessed directly, but must be swapped in and out with the main register set. The shadow registers are usually denoted by the ' symbol. They can be swapped with the following commands:

  • EX AF,AF' = Swaps AF with AF'
  • EXX = Swaps BC, DE and HL with BC', DE' and HL'

There are no shadow registers for the index registers.

Note: For a lot of operations, you can only use the A(8bit) and HL(16bit) registers. The Sjasmplus assembler has extra syntax and fake-instructions support which may produce unexpected results when source contains other than official Zilog syntax (but the parser can be configured to work in more relaxed way allowing more variations in syntax).

Instructions crossreference to X86

Here is a rough translation for some of the Z80 instructions:

  • BIT = TEST
  • CP = CMP (although the Z80 has many other handy compare functionality)
  • DJNZ = LOOP (decreases B and checks not zero)
  • EXE = Exchange all registers with Shadow registers, can be used a bit like PUSHA/POPA
  • EX = XCHG
  • HALT = HLT
  • JP = JMP
  • JR = JMP NEAR (Jump Relative)
  • LD = MOV
  • LDI = MOVSB (tmp=(HL),(DE)=tmp, DE++, HL++)
  • LDIR = REP MOVSB (tmp=(HL),(DE)=tmp, DE++, HL++, BC--)

Learning Z80 Assembler

There are many Z80 tutorials available online, but one i found very simple and clear is at this 1996 styled webpage ;-)

There is no proper index-page for this, which is why i linked all the lessons above, but you can continue to the next lesson by clicking at the next lesson at the bottom of the page.

Sinus Generator Routines

Other Resources

Also, here is a compact 'cheat sheet' with some basics for various Z80 systems: https://www.chibiakumas.com/z80/CheatSheet.pdf