Difference between revisions of "Z80 based CPUs"

From SizeCoding
Jump to: navigation, search
(Video display)
m (added links to detailed info on screen layout)
 
(11 intermediate revisions by 2 users not shown)
Line 10: Line 10:
  
 
* AF = AL + Flags
 
* AF = AL + Flags
* HL = Can be seen as BX (H=BH,L=BL) or SI in a (HL) setting, like BX also used for adressing.
+
* 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  
 
* 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
 
* DE = Can be seen as DX (D=DH,E=DL) or DI in a (DE) setting
Line 16: Line 16:
 
* IY = 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
  
For each register, there also exists a shadow register that can be swapped out by using the EX instruction.
+
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.  
 
Note: For a lot of operations, you can only use the A(8bit) and HL(16bit) registers.  
Line 53: Line 56:
  
 
== ZX Spectrum  ==
 
== ZX Spectrum  ==
The ZX Spectrum consists of a Z80A @ 3.5 MHz CPU with either 16k, 48k or 128K of RAM.
+
The ZX Spectrum consists of a Z80A @ 3.5 MHz CPU with either 16k, 48k or 128K of RAM. Most demos are targeted at the Spectrum 128 because it includes more memory, a shadow screen buffer and an AY soundchip. The different models have slightly different timings - this will cause issues if you are doing cycle-exact effects like multi-color.
  
 
=== Setting up ===
 
=== Setting up ===
Line 59: Line 62:
  
 
* 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/
 
* 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 [https://sourceforge.net/projects/fuse-emulator/ FUSE], [https://sourceforge.net/projects/unrealspeccyp/ UnrealSpeccy] and [https://www.aptanet.org/eightyone/ EightyOne] to work best for my usecase. Most emulators can read TAP, SNA and TRD files out of the box.
+
* Emulator(s): I Found [https://www.zophar.net/sinclair/zx-spin.html ZX Sping], [https://sourceforge.net/projects/fuse-emulator/ FUSE], [https://sourceforge.net/projects/unrealspeccyp/ UnrealSpeccy] and [https://www.aptanet.org/eightyone/ EightyOne] to work best for my usecase. Most emulators can read TAP, SNA and TRD files out of the box.
  
 
=== Start values ===
 
=== Start values ===
Line 78: Line 81:
 
* LLL is the line number within the cell (0..7)
 
* LLL is the line number within the cell (0..7)
  
 +
Calculating a screen address from XY coordinates is complicated due to the weird screen layout. In a larger demo you would generate a lookup table - it's usually best to avoid such calculations in small demos, but it can be done in under 30 bytes, eg: http://www.breakintoprogram.co.uk/computers/zx-spectrum/screen-memory-layout
  
 
==== Adding Color ====
 
==== Adding Color ====
Line 91: Line 95:
  
 
<syntaxhighlight lang="z80">
 
<syntaxhighlight lang="z80">
out (254),color
+
ld a,0 ; bottom three bits of a contain the color
 +
out (254),a
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Line 121: Line 126:
 
ld bc,768
 
ld bc,768
 
ldir
 
ldir
 +
</syntaxhighlight>
 +
 +
Another alternative method is to use the 128's memory paging, which provides a second screen buffer. This buffer is located at a different memeory location, but otherwise it is the same:
 +
<syntaxhighlight lang="z80">
 +
; main loop starts here
 +
ld a,00010111b ; set up memory banks and screen here
 +
mainloop
 +
halt ; sync
 +
xor 00001010b ; flip screens
 +
out ($fd),a
 +
push af
 +
 +
; render code goes here
 +
; screen buffer is location at 0xC000 instead of 0x4000
 +
 +
pop af
 +
jr mainloop
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Line 154: Line 176:
 
jp beeploop
 
jp beeploop
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
Note that the bottom three bits of port 254 also set the border color (see above).
  
 
==== Make some noise - AY ====
 
==== Make some noise - AY ====
Line 169: Line 192:
 
For more information about the soundchip, check out:
 
For more information about the soundchip, check out:
 
https://www.atarimagazines.com/v4n7/stsound.html
 
https://www.atarimagazines.com/v4n7/stsound.html
 +
 +
=== Small demos with documented source code ===
 +
* Ceci N'est Pas Un Cube by Ate Bit https://www.pouet.net/prod.php?which=29691
 +
* Zxwister by Ate Bit https://www.pouet.net/prod.php?which=44123
 +
* Muse by Ate Bit https://www.pouet.net/prod.php?which=62880
 +
* Starlet Guitarlet by HOOY-PROGRAM https://www.pouet.net/prod.php?which=52553
 +
* Clangers On The Dancefloor by HOOY-PROGRAM https://www.pouet.net/prod.php?which=29696
 +
* Chessington World Of Adventure by HOOY-PROGRAM https://www.pouet.net/prod.php?which=76074
 +
* Roll Me Gently by Joker https://www.pouet.net/prod.php?which=86338
  
 
=== Additional Resources ===
 
=== Additional Resources ===
 
I found resources on ZX Spectrum sizecoding to be sparse.
 
I found resources on ZX Spectrum sizecoding to be sparse.
 
* All kinds of Z80 Information http://www.z80.info/index.htm
 
* All kinds of Z80 Information http://www.z80.info/index.htm
 +
* Another source for good Z80 Information http://z80-heaven.wikidot.com
 
* Assembler subforum on world of spectrum https://worldofspectrum.org/forums/categories/assembler
 
* Assembler subforum on world of spectrum https://worldofspectrum.org/forums/categories/assembler
 +
* Development subforum on Spectrum Computing https://spectrumcomputing.co.uk/forums/viewforum.php?f=6
 
* Blogpost on ZX Spectrum coding from a X86 coder's perspectove on superogue's sizecdoing blog (soon)
 
* Blogpost on ZX Spectrum coding from a X86 coder's perspectove on superogue's sizecdoing blog (soon)
 
* 128byte/256 byte zx spectrum productions by goblinish https://www.pouet.net/groups.php?which=11696&order=type
 
* 128byte/256 byte zx spectrum productions by goblinish https://www.pouet.net/groups.php?which=11696&order=type
 
* 128byte/256 byte zx spectrum productions by gasman https://www.pouet.net/groups.php?which=1218&order=type
 
* 128byte/256 byte zx spectrum productions by gasman https://www.pouet.net/groups.php?which=1218&order=type
 +
* Easy to read list of all Z80 instructions (including undocumented ones) with size and timing information https://clrhome.org/table/
 +
* Detailed information on the screen layout http://www.overtakenbyevents.com/lets-talk-about-the-zx-specrum-screen-layout/ http://www.overtakenbyevents.com/lets-talk-about-the-zx-specrum-screen-layout-part-two/ and http://www.overtakenbyevents.com/lets-talk-about-the-zx-specrum-screen-layout-part-three/
  
 
== Amstrad CPC ==
 
== Amstrad CPC ==

Latest revision as of 14:08, 12 September 2020

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:

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 doesn't really do proper syntax checking for this so beware.

Instructions

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.

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


ZX Spectrum

The ZX Spectrum consists of a Z80A @ 3.5 MHz CPU with either 16k, 48k or 128K of RAM. Most demos are targeted at the Spectrum 128 because it includes more memory, a shadow screen buffer and an AY soundchip. The different models have slightly different timings - this will cause issues if you are doing cycle-exact effects like multi-color.

Setting up

Setting up your development platform for the ZX Spectrum is quite easy, first get the following tools:

Start values

Upon startup (when called from basic), the following values can assumed:

  • The alternate HL register is set to 0x2758
  • BC = start address
  • A = C

Video display

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.

Address = 010RRLLL RRRCCCCC

  • where RRRRR is the row number (0..23)
  • CCCCC is the column number (0..31)
  • LLL is the line number within the cell (0..7)

Calculating a screen address from XY coordinates is complicated due to the weird screen layout. In a larger demo you would generate a lookup table - it's usually best to avoid such calculations in small demos, but it can be done in under 30 bytes, eg: http://www.breakintoprogram.co.uk/computers/zx-spectrum/screen-memory-layout

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

Because updating pixel memory can be slow, especially when you are grasping for bytes, some of the tiny intros on the zx spectrum prefer to use the colorram for the effect, sometimes in combination with an overlaying pattern.

Border Color

You can set the border color to any of the 8 colors with:

ld a,0 ; bottom three bits of a contain the color
out (254),a

Getting something on screen

Now to get something on screen, lets fill our colorram with a simple AND pattern, like so:

 ld de,5800h
 ld b,24
yloop:
   ld c,32
xloop:
   ld a,c
   and b
   and 7    ; make sure range is 0..7
   ld (de),a
   inc de
   dec c
   jr nz,xloop
djnz yloop

Using a Backbuffer

While the above code will run fine, you might want to consider using a backbuffer for more complex stuff. You can then simply write to another adress define by BACKBUFFER (for example A000) and copy the buffer to colorram like so:

halt               ; synchronize 
ld hl,BACKBUFFER   
ld de,5800h
ld bc,768
ldir

Another alternative method is to use the 128's memory paging, which provides a second screen buffer. This buffer is located at a different memeory location, but otherwise it is the same:

	; main loop starts here
	ld	a,00010111b	; set up memory banks and screen here
mainloop
	halt			; sync
	xor	00001010b	; flip screens
	out	($fd),a
	push	af

	; render code goes here
	; screen buffer is location at 0xC000 instead of 0x4000

	pop	af
	jr	mainloop

Overlaying simple graphics

If you don't want to use a solid color/tile for. You could copy a single tile across the screen at startup for some flair.

   ld a,0x55   ; 01010101 pattern
   ld bc,0x1800
copyloop:
   ld (de),a
   inc de
   dec c
jr nz,copyloop
djnz copyloop

Alternatively you could generate a pattern using logic operations or random noise/data.

Sound

The original Spectrum has only a 1 bit sound capability (BEEP) through its internal speaker. Later models included the AY-3-8910 Soundchip which provides 3 channels of PSG sound.

Make some noise - Beeper

Setting/toggling bit 4 of port 254 will enable the beeper speaker.

		ld a,$10	;Bit 4 set
beeploop:	xor $10		;toggle Bit 4
		out (254),a
		ld b,90		;wait
		djnz $
		jp beeploop

Note that the bottom three bits of port 254 also set the border color (see above).

Make some noise - AY

You can access the AY soundchip be outputting to the following ports:

ld bc,0xfffd
ld a, ay register number
out (c),a
ld b,0xbf	
ld a, data byte
out (c),a

For more information about the soundchip, check out: https://www.atarimagazines.com/v4n7/stsound.html

Small demos with documented source code

Additional Resources

I found resources on ZX Spectrum sizecoding to be sparse.

Amstrad CPC

The Amstrad consists of a Z80A @ 3.5 MHz CPU

Setting up

Setting up..

  • Assembler: -
  • Emulator(s): -

Video Display

No information yet

Sound

No information yet

Additional Resources

No information yet