;; program 3
org &8000

;; disable interrupts
di

;; unlock asic to gain access to asic registers
ld b,&bc
ld hl,sequence
ld e,17
.seq 
ld a,(hl)
out (c),a
inc hl
dec e
jr nz,seq

;; setup for interrupt mode 1
ld a,&c3
ld hl,im1_interrupt_handler
ld (&0038),a
ld (&0039),hl

;; setup for interrupt mode 2
;; initialise bits 15..8 of interrupt vector
ld a,&40
ld i,a

;; setup interrupt handler jumpblock
ld ix,&4000
ld hl,im2_interrupt_handler
ld b,0
.sim2
ld (ix+0),l
ld (ix+1),h
inc ix
inc ix
djnz sim2

;; disable interrupts
di
;; set interrupt mode
;; {insert instruction here}
defb &ed,&4e

;; enable interrupts
ei

;; wait for interrupt
halt
;; (this is used by interrupt mode 0)
nop

di          ;; disable interrupts so we can change interrupt mode safely
im 1        ;; set Z80 interrupt mode 1 (see comment for "BRK" below)
brk         ;; this is a special instruction provided by the Maxam assembler/dissassembler/monitor
            ;; when executed a dump of the register values is displayed. This instruction assembles
            ;; to a RST 30H. This instruction uses the Amstrad firmware to execute, and the Amstrad
            ;; firmware requires interrupt mode 1.

.im1_interrupt_handler
ld b,1
ei
ret

.im2_interrupt_handler
ld b,2
ei
ret

;; this is the sequence to unlock the ASIC extra features
.sequence
defb &ff,&00,&ff,&77,&b3,&51,&a8,&d4,&62,&39,&9c,&46,&2b,&15,&8a,&cd,&ee