;MVG Module Method #1: Copy Entire Packet into Module's memory
; can be used as "inlude" file if observing proper labels and stack.

***************************************************************************
*									  *
* ------------------------ First, the legalese -------------------------- *
*									  *
* Source code Copyright (C) 1989,90 W. David Parks. All rights reserved.  *
* No part of this SOURCE CODE may be reproduced, transmitted, transcribed *
* stored in a retrieval system, or translated in any form or by any means *
* without the prior written permission of the author, W.D.Parks		  *
*									  *
*									  *
* We make no representations or warranties with respect to the contents   *
* of this document and specifically disclaim any implied warranties or    *
* merchantability, or fitness for any particular purpose.                 *
*									  *
* ----------------------------------------------------------------------- *
*									  *
*	Written permission is granted to registered owners to use         *
*       any or all of this source code in their own modules for           *
*	private use. Please contact Dr. Bobware for details for           *
*	any commercial use.						  *
*									  *
*									  *
*									  *
***************************************************************************

* last changed: 02/09/90


* this source code is intended for HiSoft's Assembler, GENST2.
* other assemblers can be used with little modification.
* the biggest requirement is it's MACRO capability.
* after having Macros, I find it quite hard to live without them.
* although I'm sure I remember a time when I did... 
* or was that just a nightmare I once had... I dunno...

;source code is copyright w.david parks, all rights reserved
;
;
 
 
;METHOD #1, Packet Access
;(Method#1 is affectionately termed: the "crazy mixed-up method")
;
;file 1 of 2 for module design
;
;packet contains pointers to true variables and routines
;MVG's variables are both word and long, their assignment
;here is rather intuitive...
;
;
;Certainly, not all the functions in the Packet will be required
;by your module. Use only the ones necessary and save memory space.
;Although... I don't bother anymore... I use Method#2, but even when
;using this method, copying the entire packet into local memory
;only requires about 1k or so... so... I quit being "byte_crunchy".
;
;
;
; pointer_offset     size of MVG var/function
;-------------------------------------
MAIN_IMAGE equ 0    ;long,buff main image buffer (actual pointer)
MAIN_LEN   equ 4    ;long,var  length of main image buffer (actual value)

*--- from this point on, all items are pointers to MVG's vars or Routines

WIDTH      equ 8    ;word, byte-width of image
HEIGHT     equ 12   ;word, raster height of image
CLIPBOARD  equ 16   ;long,buff a 0 here means no buffer (oops, actual pointer)
CLIPBFLAG  equ 20   ;word, 0= no clip exists in clipboard
CLIPBDISP  equ 24   ;rout, display clipboard
GDISPLAY   equ 28   ;rout, display main_image (see top_row/tabber)
TOP_ROW    equ 32   ;word, 1st row of main_image displayed
TABBER     equ 36   ;word, 1st TAB (offset from left, 8 pixel increments)
FULLFLAG   equ 40   ;word, used for displaying 400 rasters (vs 381)
DISP_LINS  equ 44   ;word, rasters to display (use caution)
VID_PLAY   equ 48   ;long, adrs of video memory (usually at row#19)
FILENAME   equ 52   ;buff, adrs of internal string for full pathspec
MISC_DIR   equ 56   ;buff, adrs for path string (used in f_sel)
MISC_FIL   equ 60   ;buff, adrs for filename only (used in f_sel)
ASK_MISC   equ 64   ;rout, f_sel, returns D0=-1 if error, else ok
WHEREM     equ 68   ;rout, poll mouse. D1.L=X,Y  D0.b= button_status
MOUSE_XY   equ 72   ;long, dupe of mouse X,Y. (set by wherem, same as D1)
SETSIZE    equ 76   ;rout, set dims for line_a calls (*), d0-d3=x,y,w,h
DRAWBOX    equ 80   ;rout, *draw box outline 
DRAWXBOX   equ 84   ;rout, *draw box outline (using XOR write mode)
ATOG_NRM   equ 88   ;rout, restore Normal replace write_mode
ATOG_XOR   equ 92   ;rout, *set XOR write_mode
RECTANG_Q  equ 96   ;rout, *draw a filled box
SET_LINE  equ 100   ;rout, *
MAKELINE  equ 104   ;rout, *
TOPTITLE  equ 108   ;rout, centered string in menu_bar area. A0->string
PHEX1     equ 112   ;rout, print 2-digit hexascii, D0.b
HEXIT     equ 116   ;rout, print 4-digit hexascii, D0.w
HEXITL    equ 120   ;rout, print 8-digit hexascii, D0.L
BDEC2     equ 124   ;rout, print 2-digit decimalascii, (00-99)
BDEC3     equ 128   ;rout, print 2-digit decimalascii, (000-999)
BDEC4     equ 132   ;rout, print 2-digit decimalascii, (0000-9999)
ANYKEY    equ 136   ;rout, wait for keypress, returns in D0.L
HI_STRING equ 140   ;rout, print ascii_z string, A0-> string
HI_CHAR   equ 144   ;rout, print single char in D0.b
;---------------------1.20i-------------------------------------
ORIG_PIXES equ 148  ;word, pixel width of current image
RFILENAME  equ 152  ;buff, last loaded filename
OLDREZW    equ 156  ;2wor, H/V microns (as loaded)
IREZ_W     equ 160  ;2wor, horz/vert microns for current image
STILLSEC   equ 164  ;rout, checks for image overrun into clipboard
MOTHER     equ 168  ;rout, Floyd
ALERTER    equ 172  ;rout, form_alert call. A0->alert_string, d0=button
INT_IN     equ 176  ;buff, int_in AES array used by MVG
ADDR_IN    equ 180  ;buff, addr_in ...
INT_OUT    equ 184  ;buff, int_out ...
ADDR_OUT   equ 188  ;buff, addr_out...

ADDEXT     equ 192  ;rout, add extension to filename_string   ;A0 to A1
COPYTOZ    equ 196  ;rout, copy string to string              ;A0 to A1
APPENDTO   equ 200  ;rout, append 1 string onto another       ;A0 to A1
GETPICLEN   equ 204 ;rout, fetch len of image     D0=len, D1=endofimage
YOU_ACTION  equ 208 ;
ROUND_PIXES equ 212 ;
DRAGABOX    equ 216 ;
SETMOUSEXY  equ 220 ;rout, set mouse to specific X,Y coord  D0.L=X,Y


***************************************************************************
***************************************************************************
***************************************************************************
***************************************************************************
***************************************************************************


;-------------------------------------------------------------------------
;AGRAB and GRAB must be used while packet adrs is in A6
;-------------------------------------------------------------------------
;these offered me easy access to single pointers in the packet, but since
;it is easier to copy the whole thing at once, you may forget these....
;
;
;-------------------------------- fetch adrs of packet_entry 
AGRAB macro packet#,dest_var
 move.l \1(a6),\2
 endm
;sample:
;
;AGRAB #WID,_width      _width now holds pointer to MVG's variable WID
;-------------------------------------------------------------------------
;
;-------------------------------- fetch value of packet_entry's variable
GRAB macro packet#,dest_var
 move.l \1(a6),a0
 move.\0 (a0),\2
 endm
;sample:
;
;GRAB #WID,width        width now holds image's width
;-------------------------------------------------------------------------


;------------------------------- qwik method to call an MVG routine
;                                without garbaging any registers, but
;                                cannot be used IN_LINE...
;                                see sample calls to MVG routines
subber macro whr
 push.l \1
 rts
 endm
;
;sample:
;
;gimme_a_key subber _anykey     ;"_anykey" must hold pointer to ANYKEY
;                               ;from Packet List...
;-------------------------------------------------------------------------



*****************************************************************************
**************************  MACROS for EASY Functions ***********************
*****************************************************************************
******************************* 
******************************* 
******************************* 
******************************* Lazy_Typer's MOVE.? _whatever_,-(sp)
******************************* 
******************************* ie  Push something on the stack
******************************* 
******************************* HISOFT option "\0" automatically
******************************* appends any entered size, default=W
******************************* sample:  PUSH.L #4   = move.l #4,-(sp)

push macro
 move.\0 \1,-(sp)
 endm

******************************* Lazy_Typer's MOVE.? (sp)+,_whatever_
******************************* 
******************************* ie  Pop something off the stack
******************************* 
******************************* HISOFT option "\0" automatically
******************************* appends any entered size, default=W
******************************* sample:  POP.L D7   = move.l (sp)+,D7

pop macro
 move.\0 (sp)+,\1
 endm
 
******************************* 
******************************* Lazy_Typer's MOVEM.L A0-A6/D0-D7,-(sp)
******************************* 
******************************* ie  push everything on the stack!
******************************* 

pha macro
 movem.l a0-a6/d0-d7,-(sp)
 endm
 
******************************* 
******************************* Lazy_Typer's MOVEM.L (sp)+,A0-A6/D0-D7
******************************* 
******************************* ie  pop everything back off the stack!
******************************* 

pla macro
 movem.l (sp)+,a0-a6/d0-d7
 endm
 
*****************************************************************************
**************************       End of MACROS       ***********************
*****************************************************************************



;=================================================================
;MOD_INI: Initialization routines for Module setup
;file 2 of 2 for module design
;
; 
 section text
 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; the program proper

PRG_INIT:
 bra .begin
 dc.b 'MVG Module xxxxx xxxxx xxxxx x',0    ;for future identification
 even

.begin

;----- Here we have the standard ol' PRG initialization.        --------------
;      If this is used as an INCLUDE source file, then your     --------------
;      programming need not include this, as it's already here! --------------
;-----------------------------------------------------------------------------

	move.l	4(a7),a3		base page
	move.l  a3,basepage		save, cuz this gets hard to find later
	move.l	#ustack,a7
	move.l	$c(a3),d0		text len
	add.l	$14(a3),d0		data len
	add.l	$1c(a3),d0		BSS len
	add.l	#$100,d0		basepage
	move.l	d0,-(sp)
	move.l	a3,-(sp)
	clr.w	-(sp)
	move.w	#$4a,-(sp)
	trap	#1			shrink memory
	lea	12(sp),sp


;-------------------------------------------
;start test for MVG signature -----------------------------------------------
;-------------------------------------------

        move.l $24(a3),a0            parent's basepage adrs
        move.l a0,parent
        add.l #$80,a0                A0--> parent's cmdline
        move.l (a0)+,a6              A6--> MVG's Packet
        move.l a6,mvg_packet         adrs of packet (local label)

        move.l (a0)+,d0              0,"MVG"  signature 
        move.l (a0)+,d1              "0.20"   version number (ascii)

        cmp.l MVG_SIG,d0             ;0,"MVG"
        bne badbye                   ;MVG is not the caller... no packet

        add.b #1,ernum
        cmp.l MVG_VER,d1             ;"0.20"
        blt badver                    ;MVG is older version than required.
        bgt .2a                       ;additional checking can be made on
                                      ;word following version number in
                                      ;parent's basepage, if necessary.

      move.b (a0),d0                  ;fetch character (version extension)
      cmp.b min_ver,d0                ;"h" is earliest version usable
      blt badver
.2a

;-------------------------------------------
;end test for MVG signature -----------------------------------------------
;-------------------------------------------



;-------------------------------------------
;Copy packet to local mem   -----------------------------------------------
;-------------------------------------------

read_packet:            ;copy MVG's packet into local memory space so
                        ;we can use local labels
 lea packet,a1
 move.l a6,a0
 move.l #99,d0		;gonna copy 99 long pointers (packet has fewer)
.1
 move.l (a0)+,(a1)+
 dbf d0,.1


 GRAB WIDTH,wid		; make copy of WID in local space
 GRAB HEIGHT,hgt	; make copy of HGT in local space

 clr.w cb_flag
 tst.l clipboard	; see if ClipBoard is active, may not be!
 beq .2			; a zero means that memory was tight, no ClipBoard!
 move.w #1,cb_flag	; set a local flag that clipboard is FINE
 sub.l #32000,buflen	; and reduce max size of main image buffer
.2


;---------------------- optional, for display of image from very top left
; AGRAB TOP_ROW,a0 
; clr.w (a0)             ;top_row & tabber to zero,
; AGRAB TABBER,a0
; clr.w (a0)

;---------------------- optional
; bsr gdisplay		 ;display main image, good idea but not required.



****************************************************
****************************************************
****************************************************


 jmp Main_Action	; go do your code..........


****************************************************
****************************************************
****************************************************

;-------------------------------------------
;Section for bad or missing MVG -------------------------------------------
;-------------------------------------------

badver
 lea ermsg2,a0            ;print error msg
 bsr sysprin
 add.b #1,MVG_VER         ;show required version#
 lea MVG_VER,a0
 bsr sysprin
 bra byex
 
badbye
 lea ermsg,a0          ;No Packet Provided (MVG not parent of module)
 bsr sysprin
 lea ermsg2a,a0 
 bsr sysprin
 lea MVG_VER,a0
 bsr sysprin

byex
 push.w #7
 trap #1
 addq.l #2,sp
 bra gbyeoops
gbye: 
gbyeoops
        push.w #2               ;at this time, MVG does not inquire
        push.w #$4c             ;for an error return number. This
        trap #1                 ; #2 is arbitrarily selected here.

sysprin				;print string using GEMDOS
 push.l a0
 push.w #9
 trap #1
 addq.l #6,sp
 rts
 
ermsg dc.b 27,'Y',32,32,27,'K Err# '
ernum dc.b '0   Cannot Find MVG Packet.',7,0
 even
ermsg2 dc.b 27,'Y',32,32,27,'K'
ermsg2a dc.b ' Requires MVG version # ',0
 even

                
MVG         dc.w 0,0        
MODULE_ID   dc.w 0,0

MVG_SIG     dc.b 0,'MVG'
MVG_VER     dc.b '0.20'         ;minimum version MVG to use this module
MVG_SUBVER  dc.b 'i',0          ;a sub version is always 1-byte long.
basepage    dc.l 0 
parent      dc.l 0
mvg_packet  dc.l 0
min_ver     dc.b 'i',0          ;SET REQUIRED MINIMUM CHAR HERE

wid dc.w 0			;local usage of WID
hgt dc.w 0			;ditto for HGT

cb_flag   dc.w 0		;and a flag for a valid/invalid clipboard



;-------------------- the entire packet will be copied from MVG's memory space
;-------------------- to here, in the modules memory space, by READ PACKET
packet:
bgraph    dc.l 0
buflen    dc.l 0
_wid      dc.l 0
_hgt      dc.l 0
clipboard dc.l 0
_clipflag dc.l 0
_clipdisp dc.l 0
_gdisplay dc.l 0
_top_row  dc.l 0
_tabber   dc.l 0
_fullflag dc.l 0
disp_lins dc.l 0
_vid_play dc.l 0
_filename dc.l 0
_miscdir  dc.l 0
_miscfil  dc.l 0
_askmisc  dc.l 0
_wherem   dc.l 0
_mouse_xy dc.l 0
_setsize  dc.l 0
_drawbox  dc.l 0
_drawxbox dc.l 0
_atognrm  dc.l 0
_atogxor  dc.l 0
_rectangq dc.l 0
_setline  dc.l 0
_makeline dc.l 0
_toptitle dc.l 0
_phex1    dc.l 0
_hexit    dc.l 0
_hexitl   dc.l 0
_bdec2    dc.l 0
_bdec3    dc.l 0
_bdec4    dc.l 0
_anykey   dc.l 0
_histring dc.l 0
_hichar   dc.l 0
;---------------------1.20i-------------------------------------
_origpix  dc.l 0
_rfilename dc.l 0
_oldrezw  dc.l 0
_irezw    dc.l 0
_stillsec dc.l 0
_mother   dc.l 0
_alerter  dc.l 0
_intin    dc.l 0
_addrin   dc.l 0
_intout   dc.l 0
_addrout  dc.l 0

_addext   dc.l 0
_copytoz  dc.l 0
_appendto dc.l 0
_getpiclen dc.l 0
_you_act  dc.l 0
_roundpix dc.l 0
_dragabox dc.l 0
_setmousexy dc.l 0

 dcb.l 50,0		;extra room for safety sake
 



;-------------------------------------------
;Section for accessing MVG's routines -------------------------------------
;-------------------------------------------



;---------------------  Local variables, (you may need them or... not)
mouse_xy
mouse_x dc.w 0
mouse_y dc.w 0


wherem 
 bsr .wherem2
 move.l d1,mouse_xy
 rts
.wherem2 subber _wherem


;-------- all the rest of the routines that access the Packet.
;         you may need them, or you may not...here they all are

copytoz		subber _copytoz
hi_string 	subber _histring
hi_char		subber _hichar
addext 		subber _addext
appendto	subber _appendto
bdec2		subber _bdec2
bdec3		subber _bdec3
bdec4		subber _bdec4
phex1		subber _phex1
hexit		subber _hexit
hexitl		subber _hexitl
anykey		subber _anykey
drawbox		subber _drawbox
drawxbox	subber _drawxbox
askmisc		subber _askmisc
setsize		subber _setsize
mother		subber _mother
alerter		subber _alerter
setmousexy	subber setmousexy
atognrm		subber _atognrm
atogxor		subber _atogxor
rectangq	subber _rectangq
setline		subber _setline
makeline	subber _makeline
toptitle	subber _toptitle
stillsec	subber _stillsec

gdisplay	subber _gdisplay
 
;-----------------------------

********************************************************************
********************************************************************
********************************************************************
***** CUT HERE to use as an INCLUDE file for your own modules ******
*** Just remember to use the label for the stack in the BSS area ***
****  AND have your routine start with the label: Main_Action  *****
****    OR... use whatever label you want in place of M_A...   *****
********************************************************************
********************************************************************
********************************************************************

Main_Action


;		YOUR CODE GOES HERE
;
;               but here's a sample anyway......



     bsr gdisplay         ;display the current image
     lea first_lert,a0
     moveq #1,d0
     bsr alerter
     
     cmp.w #2,d0
     beq goombye


grid_rows:
     
     move.l bgraph,a3
      
     clr.l d7
     clr.l d6
     clr.l d5
     
     move.w hgt,d7
     move.w wid,d6
     move.l d6,d5
     lsl.l #3,d5           ;every 8 rows     

.0     
     move.b #$ff,d0
     move.l a3,a2
     move.l d6,d1
     subq.w #1,d1
.1     
     move.b d0,(a2)+
     dbf d1,.1
     
     add.l d5,a3
     sub.w #8,d7
     bpl .0
     

grid_columns:
     
     move.l bgraph,a3
      
     clr.l d7
     clr.l d6
     clr.l d5
     
     move.w hgt,d7
     move.w wid,d6
     move.l d6,d5

     subq.w #1,d5
     
.0     
     move.b #$80,d0
     move.l a3,a2
     move.l d7,d1
     subq.w #1,d1
.1     
     move.b (a2),d2
     or.b d0,d2
     move.b d2,(a2)
     add.l d6,a2
     dbf d1,.1
     
     addq.l #1,a3
     dbf d5,.0
     
;     bsr anykey
     bra goombye
     nop
     



goombye
 push.w #0
 push.w #$4c		P_TERM, this is the way we exit a module
 trap #1		        (but you knew that)
 

********************************************************************
********************************************************************
********************************************************************
 section data
 even

 
;for sample module

myvideo dc.l 0

first_lert dc.b '[0]['
 dc.b ' Draw a Grid on |'
 dc.b '  the current   |'
 dc.b '    Image?      ]'
 dc.b '[Yes|No]'
 even
 
 
 section bss
 even

 ds.l 300
ustack ds.l 1




 end
 