; Constants
EXECBASE=4

; Intuition flags
CUSTOMSCREEN=$f
CUSTOMBITMAP=$40

; Exec function offsets
OPENLIBRARY=-552
CLOSELIBRARY=-414

; Intuition function offsets
OPENSCREEN=-198
CLOSESCREEN=-66

; Graphics function offsets
LOADRGB4=-192

; DOS function offsets
OPEN=-30
CLOSE=-36
READ=-42

; Hardware registers
CIAPRA=$bfe001

 SECTION code,CODE

; Open and initialize
 move.l (EXECBASE).w,a6
 move.l #IntuitionName,a1
 moveq #33,d0
 jsr OPENLIBRARY(a6)
 move.l d0,IntuitionBase
 beq Exit

 move.l (EXECBASE).w,a6
 move.l #GraphicsName,a1
 moveq #33,d0
 jsr OPENLIBRARY(a6)
 move.l d0,GfxBase
 beq CloseIntuition

 move.l (EXECBASE).w,a6
 move.l #DOSName,a1
 moveq #33,d0
 jsr OPENLIBRARY(a6)
 move.l d0,DOSBase
 beq.s CloseGraphics

 bsr.s LoadData

; Open the screen
 move.l IntuitionBase,a6
 move.l #NewScreen,a0
 jsr OPENSCREEN(a6)
 move.l d0,ScreenPtr
 beq.s CloseIntuition
 add.l #44,d0 ;ScreenPtr + 44 = ViewPortPtr

; Load the palette
 move.l d0,a0
 move.l #Palette,a1
 moveq #4,d0
 move.l GfxBase,a6
 jsr LOADRGB4(a6)

; Wait for mouse button
MouseButUp
 btst.b #6,(CIAPRA).l
 bne.s MouseButUp

; Close and exit
CloseHexes

 move.l IntuitionBase,a6
 move.l ScreenPtr,a0
 jsr CLOSESCREEN(a6)

CloseDOS
 move.l (EXECBASE).w,a6
 move.l DOSBase,a1
 jsr CLOSELIBRARY(a6)

CloseGraphics
 move.l (EXECBASE).w,a6
 move.l GfxBase,a1
 jsr CLOSELIBRARY(a6)

CloseIntuition
 move.l (EXECBASE).w,a6
 move.l IntuitionBase,a1
 jsr CLOSELIBRARY(a6)

Exit
 moveq #0,d0
 rts

; Load the data file from disk into the bitplanes and palette memory
LoadData
 move.l DOSBase,a6
 move.l #DataFileName,d1
 move.l #1005,d2 ;MODE_OLDFILE
 jsr OPEN(a6)
 move.l d0,d4
 beq.s .Abort
 move.l d4,d1
 move.l #BitPlanes,d2
 move.l #16008,d3
 jsr READ(a6)
 move.l d4,d1
 jsr CLOSE(a6)
.Abort
 rts

; PC relative data
IntuitionBase ds.l 1
GfxBase ds.l 1
DOSBase ds.l 1
ScreenPtr ds.l 1

; Initialized data
 SECTION data,DATA

NewScreen
 dc.w 0,0
 dc.w 320,200
 dc.w 2
 dc.b 1,0
 dc.w 0
 dc.w CUSTOMSCREEN+CUSTOMBITMAP
 dc.l 0
 dc.l 0
 dc.l 0
 dc.l sample.dataBMap

sample.dataBMap:
 dc.w 40 ;BytesPerRow
 dc.w 200 ;Rows
 dc.b 0 ;Flags
 dc.b 2 ;Depth
 dc.w 0 ;Pad
 dc.l BitPlanes
 dc.l BitPlanes+8000
 ds.l 6 ;Unused planes

;Palette offset=16000

DataFileName dc.b 'sample.data',0

IntuitionName dc.b 'intuition.library',0
GraphicsName dc.b 'graphics.library',0
DOSName dc.b 'dos.library',0

; Unitialized chip data
 SECTION bss_c,BSS_C
BitPlanes ds.b 40*200*2 ;bytesperrow*rows*bitplanes
Palette ds.b 4*2 ;Number of colors*2
