 bin
 ttl Primitive drawing routines for L1 Graphics
 incl inc:module.asm
stack set -64
 module grafpack

******************************************
*                                        *
* Primitive drawing routines for level 1 *
* graphics.                              *
*                                        *
* Copyright M.R.King 26/3/1984           *
* All rights reserved                    *
*                                        *
******************************************

fsbase   set     $E00000
fspwrten set     fsbase+$20000     bits 0-3 of byte
fscolour set     fsbase+$20001     bits 0-3 of byte
fsorigin set     fsbase+$20002     X bits 0-5, Y bits 6-15

black    set     $0
red      set     $1
green    set     $2
yellow   set     $3
blue     set     $4
magenta  set     $5
cyan     set     $6
white    set     $7

movel30  macro
\*       move.l  \1,(\2)+   1
         move.l  \1,(\2)+   2
         move.l  \1,(\2)+   3
         move.l  \1,(\2)+   4
         move.l  \1,(\2)+   5
         move.l  \1,(\2)+   6
         move.l  \1,(\2)+   7
         move.l  \1,(\2)+   8
         move.l  \1,(\2)+   9
         move.l  \1,(\2)+   10
         move.l  \1,(\2)+   11
         move.l  \1,(\2)+   12
         move.l  \1,(\2)+   13
         move.l  \1,(\2)+   14
         move.l  \1,(\2)+   15
         move.l  \1,(\2)+   16
         move.l  \1,(\2)+   17
         move.l  \1,(\2)+   18
         move.l  \1,(\2)+   19
         move.l  \1,(\2)+   20
         move.l  \1,(\2)+   21
         move.l  \1,(\2)+   22
         move.l  \1,(\2)+   23
         move.l  \1,(\2)+   24
         move.l  \1,(\2)+   25
         move.l  \1,(\2)+   26
         move.l  \1,(\2)+   27
         move.l  \1,(\2)+   28
         move.l  \1,(\2)+   29
         move.l  \1,(\2)+   30
         endm

         code
         dc.b    'Level 1 Graphics primitive routines. '
         dc.b    'Copyright M.R.King, 26/3/1984. '
         dc.b    'All rights reserved.'
         ds.w    0
vline3   move.w  ccr,-(sp)    d0=x, d1=y1, d2=y2
         movem.l d0-d3/a0,-(sp)
         move.l  #$03FF,d3
         and.l   d3,d1
         and.l   d3,d2
         move.w  d0,d3
         and.w   #$03F0,d0
         lsr.w   #3,d0
         lea     fsbase,a0
         add.w   d0,a0
         not.w   d3
         and.w   #$F,d3
         clr.w   d0
         bset.l  d3,d0
         sub.w   d1,d2
         bcc.s   vlininy3
         add.w   d2,d1
         neg.w   d1
vlininy3 lsl.l   #7,d1
         add.l   d1,a0
         move.w  #128,d1
         lsr.w   #1,d2
         bcc.s   vlinele3
vlinelp3 move.w  d0,(a0)
         add.l   d1,a0
vlinele3 move.w  d0,(a0)
         add.w   d1,a0
         dbra    d2,vlinelp3
         movem.l (sp)+,d0-d3/a0
         rtr

hline3   move.w  ccr,-(sp)    d0=x1, d1=x2, d2=y
         movem.l d0-d4/a0,-(sp)
         move.l  #$03FF,d3
         and.w   d3,d0
         and.w   d3,d1
         and.l   d3,d2
         lsl.l   #7,d2
         lea     fsbase,a0
         add.l   d2,a0
         cmp.w   d0,d1
         bcc.s   hlininx3
         exg     d0,d1
hlininx3 move.w  d0,d2
         and.w   #$001F,d2
         move.l  #-1,d3
         lsr.l   d2,d3
         move.w  d1,d2
         not.w   d2
         and.w   #$001F,d2
         move.l  #-1,d4
         lsl.l   d2,d4
         and.w   #$03E0,d0
         and.w   #$03E0,d1
         lsr.w   #3,d0
         lsr.w   #3,d1
         add.w   d0,a0
         sub.w   d1,d0
         bne.s   hlinbig3
         and.l   d3,d4
         move.l  d4,(a0)
         movem.l (sp)+,d0-d4/a0
         rtr
hlinbig3 asr.w   d0
         move.l  d3,(a0)+
         move.l  #-1,d1
         jmp     hlinend3(d0.w)
         movel30 d1,a0
         move.l  d4,(a0)
hlinend3 movem.l (sp)+,d0-d4/a0
         rtr

fill3    move.w  ccr,-(sp)    d0=x1, d1=y1, d2=x2, d3=y2
         movem.l d0-d5/a0-a2,-(sp)
         move.l  #$03FF,d4
         and.w   d4,d0
         and.l   d4,d1
         and.w   d4,d2
         and.l   d4,d3
         lea     fsbase,a1
         cmp.w   d0,d2
         bcc.s   boxincx3
         exg     d0,d2
boxincx3 sub.w   d1,d3
         bcc.s   boxincy3
         add.w   d3,d1
         neg.w   d3
boxincy3 lsl.l   #7,d1
         add.l   d1,a1
         move.w  d0,d1
         and.w   #$001F,d1
         move.l  #-1,d4
         lsr.l   d1,d4
         move.w  d2,d1
         not.w   d1
         and.w   #$001F,d1
         move.l  #-1,d5
         lsl.l   d1,d5
         move.w  #128,d1
         and.w   #$03E0,d0
         and.w   #$03E0,d2
         lsr.w   #3,d0
         lsr.w   #3,d2
         add.w   d0,a1
         sub.w   d2,d0
         bne.s   bigbox3
         and.l   d4,d5
smalblp3 move.l  d5,(a1)
         add.w   d1,a1
         dbra    d3,smalblp3
         movem.l (sp)+,d0-d5/a0-a2
         rtr

bigbox3  asr.w   #1,d0
         lea     boxend3(d0.w),a2
         move.l  #-1,d0
boxloop3 move.l  a1,a0
         move.l  d4,(a0)+
         jmp     (a2)
         movel30 d0,a0
         move.l  d5,(a0)
boxend3  add.w   d1,a1
         dbra    d3,boxloop3
         movem.l (sp)+,d0-d5/a0-a2
         rtr

clear3   move.w  ccr,-(sp)
         movem.l d0-d1/a0,-(sp)
         move.w  #$FF00,fspwrten
         move.w  #$03FF,d1
         move.l  #-1,d0
         lea     fsbase,a0
fsclrlp3 movel30 d0,a0
         move.l  d0,(a0)+
         move.l  d0,(a0)+
         dbra    d1,fsclrlp3
         movem.l (sp)+,d0-d1/a0
         rtr

plot3    move.w  ccr,-(sp)     d0=x, d1=y
         movem.l d0-d1/a0,-(sp)
         lea     fsbase,a0
         and.l   #$03FF,d1
         lsl.l   #7,d1
         add.l   d1,a0
         move.w  d0,d1
         lsr.w   #3,d1
         and.w   #$007F,d1
         and.w   #$0007,d0
         move.b  bitab(d0.w),0(a0,d1.w)
         movem.l (sp)+,d0-d1/a0
         rtr
bitab    dc.b    %10000000,%01000000
         dc.b    %00100000,%00010000
         dc.b    %00001000,%00000100
         dc.b    %00000010,%00000001
         ds.w    0
***********************************************
*                                             *
* paint copies a rectangular raster from      *
* main memory to the frame store.             *
*                                             *
* d0 = x0                                     *
* d1 = x0                                     *
* d2 = x1                                     *
* d3 = y1                                     *
* d4 = bit offset for source raster           *
* d5 = line stride for source in bytes        *
* a0 = start of source raster.                *
*                                             *
***********************************************
paint3   move.w  ccr,-(sp)
         movem.l d0-d7/a0-a5,-(sp)
         move.l  #$03FF,d6
         and.w   d6,d0
         and.l   d6,d1
         and.w   d6,d2
         and.l   d6,d3
         and.w   #$000F,d4
         lea     fsbase,a1
         cmp.w   d0,d2
         bcc.s   paintinx
         exg     d0,d2
paintinx sub.w   d1,d3
         bcc.s   paintiny
         add.w   d3,d1
         neg.w   d3
paintiny lsl.l   #7,d1
         add.l   d1,a1
         move.w  d0,d1
         and.w   #$000F,d1
         sub.w   d1,d4
         move.l  #$FFFF,d6
         lsr.w   d1,d6
         move.w  d2,d1
         not.w   d1
         and.w   #$000F,d1
         move.l  #-1,d7
         lsl.w   d1,d7
         move.w  #128,a5
         and.w   #$03F0,d0
         and.w   #$03F0,d2
         lsr.w   #3,d0
         lsr.w   #3,d2
         add.w   d0,a1
         sub.w   d0,d2
         bne.s   paintb
         and.l   d6,d7
         neg.w   d4
         add.w   #16,d4
spaintlp move.l  (a0),d0
         lsr.l   d4,d0
         and.w   d7,d0
         move.w  d0,(a1)
         add.l   d5,a0
         add.l   a5,a1
         dbra    d3,spaintlp
         movem.l (sp)+,d0-d7/a0-a5
         rtr
paintb   lsr.w   #1,d2
         sub.w   #1,d2
         move.w  d2,a4
         neg.w   d4
         bpl.s   bpaintlp
         and.w   #15,d4
cpaintlp move.l  a0,a2
         move.l  a1,a3
         move.w  a4,d2
         move.l  (a2)+,d0
         ror.l   d4,d0
         and.l   d6,d0
         bra.s   paintlpt
jpaintlp move.l  #0,d1
         move.w  (a2)+,d1
         ror.l   d4,d1
         clr.w   d0
         swap    d0
         or.l    d1,d0
paintlpt move.w  d0,(a3)+
         dbra    d2,jpaintlp
         move.w  (a2),d1
         ror.w   d4,d1
         swap    d0
         or.w    d1,d0
         and.w   d7,d0
         move.w  d0,(a3)
         add.l   d5,a0
         add.l   a5,a1
         dbra    d3,cpaintlp
         movem.l (sp)+,d0-d7/a0-a5
         rtr
bpaintlp move.l  a0,a2
         move.l  a1,a3
         move.w  a4,d2
         move.l  #0,d0
         move.w  (a2)+,d0
         ror.l   d4,d0
         and.w   d6,d0
         bra.s   paintlps
ipaintlp move.l  #0,d1
         move.w  (a2)+,d1
         ror.l   d4,d1
         clr.w   d0
         swap    d0
         or.l    d1,d0
paintlps move.w  d0,(a3)+
         dbra    d2,ipaintlp
         move.w  (a2),d1
         ror.w   d4,d1
         swap    d0
         or.w    d1,d0
         and.w   d7,d0
         move.w  d0,(a3)
         add.l   d5,a0
         add.l   a5,a1
         dbra    d3,bpaintlp
         movem.l (sp)+,d0-d7/a0-a5
         rtr

line3    move.w  ccr,-(sp)     d0=x1,d1=y1,d2=x2,d3=y2
         movem.l d0-d5/a0,-(sp)
         lea     fsbase,a0
         move.l  #$03FF,d4
         and.w   d4,d0
         and.l   d4,d1
         and.w   d4,d2
         and.l   d4,d3
         sub.w   d1,d3
         beq     lineh
         bcc.s   lineincy
         exg     d0,d2
         add.w   d3,d1
         neg.w   d3
lineincy move.w  #128,d4
         sub.w   d0,d2
         beq     linev
         bcc.s   lineincx
         add.w   d2,d0
         neg.w   d2
         add.w   d3,d1
         neg.w   d4
lineincx lsl.l   #7,d1
         add.l   d1,a0
         move.w  d0,d1
         lsr.w   #3,d1
         and.w   #$007E,d1
         add.w   d1,a0
         move.l  #0,d1
         not.w   d0
         and.w   #$000F,d0
         bset.l  d0,d1
         cmp.w   d2,d3
         beq.s   lined
         bcc.s   linedy

         move.w  d2,d0
         move.w  d2,d5
         add.w   d2,d2
         add.w   d3,d3
         bra.s   linedxst
linedxlp sub.w   d3,d0
         bcc.s   linedxsk
         add.w   d2,d0
         add.w   d4,a0
linedxsk ror.w   #1,d1
         bcc.s   linedxst
         add.l   #2,a0
linedxst move.w  d1,(a0)
         dbra    d5,linedxlp
         movem.l (sp)+,d0-d5/a0
         rtr

linedy   move.w  d3,d0
         move.w  d3,d5
         add.w   d2,d2
         add.w   d3,d3
         bra.s   linedyst
linedylp sub.w   d2,d0
         bcc.s   linedysk
         add.w   d3,d0
         ror.w   #1,d1
         bcc.s   linedysk
         add.l   #2,a0
linedysk add.w   d4,a0
linedyst move.w  d1,(a0)
         dbra    d5,linedylp
         movem.l (sp)+,d0-d5/a0
         rtr

linedlp  ror.w   #1,d1
         bcc.s   linedskp
         add.l   #2,a0
linedskp add.w   d4,a0
lined    move.w  d1,(a0)
         dbra    d2,linedlp
         movem.l (sp)+,d0-d5/a0
         rtr

lineh    lsl.l   #7,d1
         add.l   d1,a0
         cmp.w   d0,d2
         bcc.s   linehinx
         exg     d0,d2
linehinx move.w  d0,d1
         and.w   #$001F,d1
         move.l  #-1,d3
         lsr.l   d1,d3
         move.w  d2,d1
         not.w   d1
         and.w   #$001F,d1
         move.l  #-1,d4
         lsl.l   d1,d4
         and.w   #$03E0,d0
         and.w   #$03E0,d2
         lsr.w   #3,d0
         lsr.w   #3,d2
         add.w   d0,a0
         sub.w   d2,d0
         bne.s   linehbig
         and.l   d3,d4
         move.l  d4,(a0)
         movem.l (sp)+,d0-d5/a0
         rtr
linehbig asr.w   d0
         move.l  d3,(a0)+
         move.l  #-1,d2
         jmp     linehend(d0.w)
         movel30 d2,a0
         move.l  d4,(a0)
linehend movem.l (sp)+,d0-d5/a0
         rtr

linev    move.w  d0,d2
         and.w   #$03F0,d0
         lsr.w   #3,d0
         add.w   d0,a0
         not.w   d2
         and.w   #$000F,d2
         move.l  #0,d0
         bset.l  d2,d0
         lsl.l   #7,d1
         add.l   d1,a0
         lsr.w   #1,d3
         bcc.s   linevle
linevlp  move.w  d0,(a0)
         add.w   d4,a0
linevle  move.w  d0,(a0)
         add.l   d4,a0
         dbra    d3,linevlp
         movem.l (sp)+,d0-d5/a0
         rtr

triangle3 move.w  ccr,-(sp)
         movem.l d0-d7,-(sp)
         cmp.w   d1,d3
         bge.s   tri2big3
         cmp.w   d1,d5
         bge.s   trifswp3
         exg     d0,d4
         exg     d1,d5
         bra.s   trifinc3
tri2big3 cmp.w   d3,d5
         bge.s   trihard3
         exg     d2,d4
         exg     d3,d5
trifinc3 cmp.w   d1,d3
         bge.s   trihard3
trifswp3 exg     d0,d2
         exg     d1,d3
trihard3 cmp.w   d3,d5
         beq.s   trilow3
         cmp.w   d1,d3
         beq.s   trihigh3
         move.w  d3,d6
         sub.w   d1,d6
         move.w  d4,d7
         sub.w   d0,d7
         muls    d6,d7
         move.w  d5,d6
         sub.w   d1,d6
         add.l   d7,d7
         divs    d6,d7
         add.w   #1,d7
         asr.w   #1,d7
         add.w   d0,d7
         exg     d0,d7
         move.w  d1,d6
         move.w  d2,d1
         move.w  d3,d2
         move.w  d4,d3
         bsr.s   trapeze3
         move.w  d0,d3
         move.w  d1,d4
         move.w  d2,d5
         move.w  d7,d0
         move.w  d0,d1
         move.w  d6,d2
         bra.s   tritrap3
trilow3  move.w  d2,d3
         move.w  d1,d2
         move.w  d0,d1
         bra.s   tritrap3
trihigh3 move.w  d2,d1
         move.w  d3,d2
         move.w  d4,d3
tritrap3 bsr.s   trapeze3
         movem.l (sp)+,d0-d7
         rtr

trapeze3 move.w  ccr,-(sp)
         movem.l d0-d7/a0-a5,-(sp)
         move.l  #$03FF,d6
         and.w   d6,d0
         lsl.w   #2,d0
         and.w   d6,d1
         lsl.w   #2,d1
         and.l   d6,d2
         and.w   d6,d3
         lsl.w   #2,d3
         and.w   d6,d4
         lsl.w   #2,d4
         and.l   d6,d5
         lea     fsbase,a1
         lea     rtab,a5
         move.w  #128,a2
         cmp.w   d0,d1
         bcc.s   trpincb3
         exg     d0,d1
trpincb3 cmp.w   d3,d4
         bcc.s   trpinct3
         exg     d3,d4
trpinct3 sub.w   d2,d5
         beq     trpline3
         bge.s   trpincy3
         add.w   d5,d2
         neg.w   d5
         exg     d0,d3
         exg     d1,d4
trpincy3 lsl.l   #7,d2
         add.l   d2,a1
         sub.w   d0,d3
         ext.l   d3
         sub.w   d1,d4
         ext.l   d4
         move.w  d5,d7
         add.w   d7,d7
         divs    d5,d3
         move.l  d3,d6
         swap    d3
         clr.w   d3
         clr.w   d6
         divs    d7,d6
         ext.l   d6
         add.l   d6,d6
         add.l   d6,d3
         divs    d5,d4
         move.l  d4,d6
         swap    d4
         clr.w   d4
         clr.w   d6
         divs    d7,d6
         ext.l   d6
         add.l   d6,d6
         add.l   d6,d4
         move.l  d3,a3
         move.l  d4,a4
         swap    d0
         swap    d1
         move.l  #-1,d7
         move.w  #$8000,d0
         move.w  d0,d1
         tst.l   d3
         bpl.s   trpincl3
         asr.l   #1,d3
         add.l   d3,d0
         move.l  d0,d3
         bra.s   trptst43
trpincl3 move.l  d3,d6
         move.l  d0,d3
         asr.l   #1,d6
         sub.l   d6,d0
trptst43 tst.l   d4
         bpl.s   trpincr3
         move.l  d4,d6
         move.l  d1,d4
         asr.l   #1,d6
         sub.l   d6,d1
         bra.s   trplpst3
trpincr3 asr.l   #1,d4
         add.l   d4,d1
         move.l  d1,d4
         bra.s   trplpst3
trpline3 lsl.l   #7,d2
         add.l   d2,a1
         move.l  #-1,d7
         cmp.w   d0,d3
         ble.s   trpbtms3
         exg     d0,d3
trpbtms3 cmp.w   d1,d4
         bge.s   trptopb3
         exg     d1,d4
trptopb3 swap    d3
         swap    d4
         bra.s   trplpst3
trploop3 tst.w   d5
         bne.s   trpnorm3
         move.l  a4,d4
         bmi.s   traprok3
         asr.l   #1,d4
         sub.l   d4,d1
traprok3 move.l  a3,d3
         bpl.s   trpnorm3
         asr.l   #1,d3
         sub.l   d3,d0
trpnorm3 move.l  d0,d3
         move.l  d1,d4
trplpst3 swap    d3
         swap    d4
         move.w  d3,d6
         move.w  d4,d2
         and.w   #$007C,d6
         and.w   #$007C,d2
         and.w   #$0F80,d3
         and.w   #$0F80,d4
         lsr.w   #5,d3
         lsr.w   #5,d4
         lea     0(a1,d3),a0
         sub.w   d4,d3
         bne.s   trphuge3
         move.l  -128(a5,d6.w),d6
         and.l   0(a5,d2.w),d6
         move.l  d6,(a0)
         add.l   a3,d0
         add.l   a4,d1
         add.l   a2,a1
         dbra    d5,trploop3
         movem.l (sp)+,d0-d7/a0-a5
         rtr
trphuge3 asr.w   #1,d3
         move.l  -128(a5,d6.w),(a0)+
         jmp     trpbase3+2(d3.w)
         movel30 d7,a0
trpbase3 move.l  0(a5,d2.w),(a0)
         add.l   a3,d0
         add.l   a4,d1
         add.l   a2,a1
         dbra    d5,trploop3
         movem.l (sp)+,d0-d7/a0-a5
         rtr
         dc.l    %11111111111111111111111111111111
         dc.l    %01111111111111111111111111111111
         dc.l    %00111111111111111111111111111111
         dc.l    %00011111111111111111111111111111
         dc.l    %00001111111111111111111111111111
         dc.l    %00000111111111111111111111111111
         dc.l    %00000011111111111111111111111111
         dc.l    %00000001111111111111111111111111
         dc.l    %00000000111111111111111111111111
         dc.l    %00000000011111111111111111111111
         dc.l    %00000000001111111111111111111111
         dc.l    %00000000000111111111111111111111
         dc.l    %00000000000011111111111111111111
         dc.l    %00000000000001111111111111111111
         dc.l    %00000000000000111111111111111111
         dc.l    %00000000000000011111111111111111
         dc.l    %00000000000000001111111111111111
         dc.l    %00000000000000000111111111111111
         dc.l    %00000000000000000011111111111111
         dc.l    %00000000000000000001111111111111
         dc.l    %00000000000000000000111111111111
         dc.l    %00000000000000000000011111111111
         dc.l    %00000000000000000000001111111111
         dc.l    %00000000000000000000000111111111
         dc.l    %00000000000000000000000011111111
         dc.l    %00000000000000000000000001111111
         dc.l    %00000000000000000000000000111111
         dc.l    %00000000000000000000000000011111
         dc.l    %00000000000000000000000000001111
         dc.l    %00000000000000000000000000000111
         dc.l    %00000000000000000000000000000011
         dc.l    %00000000000000000000000000000001
rtab     dc.l    %10000000000000000000000000000000
         dc.l    %11000000000000000000000000000000
         dc.l    %11100000000000000000000000000000
         dc.l    %11110000000000000000000000000000
         dc.l    %11111000000000000000000000000000
         dc.l    %11111100000000000000000000000000
         dc.l    %11111110000000000000000000000000
         dc.l    %11111111000000000000000000000000
         dc.l    %11111111100000000000000000000000
         dc.l    %11111111110000000000000000000000
         dc.l    %11111111111000000000000000000000
         dc.l    %11111111111100000000000000000000
         dc.l    %11111111111110000000000000000000
         dc.l    %11111111111111000000000000000000
         dc.l    %11111111111111100000000000000000
         dc.l    %11111111111111110000000000000000
         dc.l    %11111111111111111000000000000000
         dc.l    %11111111111111111100000000000000
         dc.l    %11111111111111111110000000000000
         dc.l    %11111111111111111111000000000000
         dc.l    %11111111111111111111100000000000
         dc.l    %11111111111111111111110000000000
         dc.l    %11111111111111111111111000000000
         dc.l    %11111111111111111111111100000000
         dc.l    %11111111111111111111111110000000
         dc.l    %11111111111111111111111111000000
         dc.l    %11111111111111111111111111100000
         dc.l    %11111111111111111111111111110000
         dc.l    %11111111111111111111111111111000
         dc.l    %11111111111111111111111111111100
         dc.l    %11111111111111111111111111111110
         dc.l    %11111111111111111111111111111111

printstring equ $10c4
freestore   equ $3ff0
nl          equ 10

define macro
   lea \1,a0
   move.l #\2,d0
   bsr defext
   endm

begin equ *
   define line3,$1170
   define trapeze3,$1174
   define triangle3,$1178
   define paint3,$11a0
   define hline3,$11a4
   define fill3,$11a8
   define clear3,$11ac
   define vline3,$11b0
*  define plot3,$11b4
   lea begin,a0
   move.l a0,freestore
   lea done,a0
   jsr printstring
reset rts

defext exg d0,a0
   add.w 2(a0),a0
   move.l d0,4(a0)
   rts

done dc.b end-*-1,'GRAFPACK loaded',nl,0
end equ *
   ds.w 0
   endmodule
   end
