SpectNet IDE

Visual Studio 2017/2019 integrated ZX Spectrum IDE for the Community

Tutorials v2 » Create a Z80 Program

In this article, you will learn that SpectNetIde provides you a straightforward way to create and run Z80 assembly programs.

To create your first Z80 assembly program, follow these steps:

  1. Create a new ZX Spectrum 48 project (see details here). The CodeFiles project of the folder contains a Code.z80asm file:
; Put your Z80 assembly code into code files
    .model Spectrum48
    .org #8000
    ld a,2;	      ; upper screen
    call #1601    ; open the channel
    ld hl,Message ; HL points the the message string
    ld a,(hl)     ; get next character
    or a
    jp z,#12a9    ; jump back to main cycle when terminated
    rst #10       ; display character
    inc hl        ; next character
    jr NextCh     ; next loop

    .dm "\a\x0A\x08" ; AT 10, 4
    .dm "\p\x04"     ; INK 4
    .dm "ZX Spectrum IDE"
    .db 0x00         ; terminate
  1. Insert these code lines right after .org #8000:
	ld a,3        ; purple
	out (#fe),a   ; set the border color
  1. In Solution Explorer, right-click the Code.z80asm file, and invoke the Run Z80 program command:

Run Z80 code command

Note: You can use the Ctrl+M, Ctrl+R double shortcut keys to execute the Run Z80 program.

This command compiles the Z80 assembly code to binary machine code, starts (or restarts) the Spectrum virtual machine, injects the binary code, and runs it:

Z80 code runs

You have just created your first Z80 assembly program in SpectNetIde!

Errors in the code

If you make an error – either syntax or semantic error – the SpectNetIDE assembler gives an error message. Let’s assume you make an error in the code:

; Code file
    org #8000
    ld a,hl      ; "hl" is a semantic error
    out (#fe),a 
    jp #12a2

The Z80 Assembler displays the issue in the Error List:

Compile error

When you double click the error line, the IDE navigates you to the error line in the source code:

Error marked in source

A Bit Longer Z80 Program

If you’re excited, you can try a bit longer Z80 code with border manipulations and delays. When you run it, the code sets the screen colors and uses the HALT statement to synchronized border drawing. When you start it, it runs until you press SPACE:

Z80 baner runs

Here is the entire source code:

; Define symbols
LAST_K:		.equ #5c08
MAIN_EX:	.equ #12a2

    .org #8000

; Set Banner lines
    call ClearScreen
    ld a,0b00_010_010
    ld b,0
    call ClearLine
    ld a,0b00_110_110
    ld b,1
    call ClearLine
    ld a,0b00_101_101
    ld b,2
    call ClearLine
    ld a,0b00_100_100
    ld b,3
    call ClearLine

; Set banner border area
    ld bc,#200
    call LongDelay
    ld a,2
    call SetBorder
    ld a,6
    call SetBorder
    ld a,5
    call SetBorder
    ld a,4
    call SetBorder
    ld a,0
    call SetBorder
    halt        ; Wait for the next interrupt
    ld hl,LAST_K
    ld a, (hl)	; put last keyboard press into A
    cp #20		; was "space" pressed?
    jr nz,Top	; If not, back to the cycle
    ld a,7
    call SetBorder
    jp MAIN_EX

; Sets the entire screen to black
    ld bc,24*32-1 ; #of screen attribute bytes
    ld hl,#5800 ; First attr address
    ld de,#5801 ; Next attr address
    ld (hl),0   ; Black on Black
    ldir        ; Set all attribute bytes

; A = Attr byte to set
; B = Line index
    push af     ; Save A
    ld a,b      ; Multiply the line count with 32
    sla a
    sla a
    sla a
    sla a
    sla a
    ld hl,#5800 ; Calculate the attribute address
    ld d,0
    ld e,a
    add hl,de
    pop af      ; Restore A
    ld b,#20    ; Set all the 32 attribute bytes
    ld (hl),a
    inc hl
    djnz SetAttr

; Sets the border color
    out (#fe),a
    ld b,#86
    djnz Delay

; Delays code execution
    dec bc
    ld a,b
    or c
    jr nz,LongDelay

SpectNetIDE makes it extremely simple to reuse the Z80 assembly code, as you will learn in the next article.