     opt   l-,c+,d-         nolink,case dependant,debug off
*********************
* "CMM" Console Message Monitor Demo by Maurice St. Sauveur
*    edited by JHH Lowengard
*
* This program has two purposes: 
*
*  To demonstrate the no-so-intuitive call to RawKeyConvert
*  To provide a handy way to see the real bytes straight out of Intuition
*    for Code, Class, Mouse position, Time etc. etc.
*  (It also provides a pretty good demo of RawDoFormat)
* One exits the program by pressing both Amiga Keys.
*
*  The window leaves ten pixels on the top of the screen so you can use 
*  the screen's back-front gadgets and drag bar.
*
* This program is meant to assemble under Hisoft's GenAm and GenIm
* assemblers. Your Assembler may require you to make some changes
* or option changes. Inline EQU's help speed the assembly: no
* ,include files are needed.
*
*
* This program is copyright 1989 by Maurice St. Sauveur 
* In theory, there is no part of this program which couldn't 
* have been figured out by deep scrutiny of the relevant Rom Kernal
* Guides, but in practice this is not true. 
*
* Maurice St. Sauveur is also working on on an integrated SMTE
* single frame edit system including reader, generator, and
* edit Decision List software. This system is available now,
* but might need to be customized to your studio's needs.
* For more information,
* contact :
*
* SMPTE ExorCiser
* Maurice St. Sauveur
* 100 Henry St, Suite 1229
* Brooklyn, N.Y. 11201
* (718) 624-5000, x1229
*
**********************    library vector equates         
               
_LVOGetMsg          equ   -372
_LVOReplyMsg        equ   -378
_LVOWaitPort		equ	-384
_LVOOpenLibrary     equ   -552
_LVOCloseLibrary    equ   -414      
_LVOClose           equ   -36
_LVOWait            equ   -318
_LVOOpenWindow      equ   -204
_LVOCloseWindow     equ   -72
_LVOOpenScreen      equ   -198
_LVOCloseScreen     equ   -66
_LVOMove            equ   -240
_LVORectFill        equ   -306
_LVOText            equ   -60
_LVOSetAPen         equ   -342

_LVOSetBPen         equ   -348
_LVOOpen            equ   -30
_LVORead            equ   -42
_LVOWrite           equ   -48
_LVODraw            equ   -246
_LVOSetRGB4         equ   -288
_LVOViewPortAddress equ   -300
_LVOOpenFont        equ   -72
_LVOCloseFont       equ   -78
_LVOSetFont         equ   -66
_LVODisable         equ   -120
_LVOEnable          equ   -126
_LVOModifyIDCMP     equ   -150
_LVORawKeyConvert   equ   -48
_LVOOpenDevice      equ   -444
_LVOCloseDevice     equ   -450
_LVORawDoFmt        equ   -522

LIBRARY_VERSION     equ   31


im_Class            equ   20
im_SubClass         equ   22
im_Code             equ   24
im_Qualifier        equ   26
im_Mousex           equ   32
im_Mousey           equ   34
nw_Screen           equ   $001e
wd_UserPort         equ   $0056


**********************    intuition equates
      even
REPORTMOUSE         equ       $0200
MOUSEBUTTONS        equ       $00000008
MOUSEMOVE           equ       $00000010
MENUPICK            equ       $00000100
CLOSEWINDOW         equ       $00000200
RAWKEY              equ       $00000400   *
WBENCHMESSAGE       equ       $00020000
VANILLAKEY          equ       $00200000   *
INTUITICKS          equ       $00400000

DISKREMOVED         equ       $10000
DISKINSERTED        equ       $08000
GIMMEZEROZERO       equ       $0400
WINDOWSIZING        equ       $0001       *
WINDOWDRAG          equ       $0002       *
WINDOWDEPTH         equ       $0004       *
WINDOWCLOSE         equ       $0008       *
BACKDROP            equ       $0100       *
BORDERLESS          equ       $0800       *      
ACTIVATE            equ       $1000       *
WINDOWACTIVE        equ       $2000
NOCAREREFRESH       equ       $00020000   *
WINDOWREFRESH       equ       $01000000
WBENCHWINDOW        equ       $02000000
WINDOWTICKED        equ       $04000000
CUSTOMSCREEN        equ       $000f       *
WBENCHSCREEN        equ       $0001
TOPAZ_SIXTY         equ       9
TOPAZ_EIGHTY        equ       8

idcmp1: equ    RAWKEY!CLOSEWINDOW!MOUSEBUTTONS!DISKINSERTED!DISKREMOVED!MOUSEMOVE
idcmp2: equ    VANILLAKEY!CLOSEWINDOW

Wflags: equ    ACTIVATE
Modes:  equ    $8000

**********************   external ref library

AbsExecBase        equ      $4
MODE_NEWFILE       equ      1006
MODE_READWRITE     equ      1004
*
************************** START OF PROGRAM *******************
*
* ----------------- open intuition library
          even
start:    move.l    a7,savreturn
          movea.l   #IntuitionName,a1
          move.l    #LIBRARY_VERSION,d0
          move.l    AbsExecBase,a6
          jsr       _LVOOpenLibrary(a6)
          move.l    d0,IntBase
          beq       cls_ilib

*  -------------------  open gfx libary

          movea.l   #GfxName,a1
          move.l    #LIBRARY_VERSION,d0
       
          move.l    AbsExecBase,a6
          jsr       _LVOOpenLibrary(a6)
          move.l    d0,GfBase
          beq       cls_glib
 
*  ---------------------- open screen
scr:      
          movea.l   #NewCustScreen,a0
          movea.l   IntBase,a6
          jsr       _LVOOpenScreen(a6)
          move.l    d0,CustScr
          beq       cls_sc

*  -----------------    open window
      
          movea.l   #NewWdw,a0
          move.l    CustScr,nw_Screen(a0)
          movea.l   IntBase,a6
          jsr       _LVOOpenWindow(a6)
          move.l    d0,Wdw
          beq       cls_wdw

* -------------------- get rastport

          move.l    d0,a6
          move.l    50(a6),Rp

* -------------------- get viewport

vp2:      move.l    d0,a0
          movea.l   IntBase,a6
          jsr       _LVOViewPortAddress(a6) 
          move.l    d0,Vp

*  ----------------- open console libary
* to use RawKeyConvert(), we need a base which comes from
* a special call to the Console Device.
*
oc:
          lea       CName,a0
          move.l    #-1,d0
          lea       IoRequest,a1
          move.l    Wdw,(a1)
          move.l    #0,d1
          movea.l   AbsExecBase,a6
          jsr       _LVOOpenDevice(a6)
          move.l    20(a1),ConDbase
          move.l    ConDbase,d0

* ------------ get rawkey from intuition message
* Waiting for the message is less rude than busy polling
*
wfm:      movea.l   Wdw,a0
          movea.l   wd_UserPort(a0),a0
          movea.l   AbsExecBase,a6
          jsr       _LVOWaitPort(a6)        * get any mail?
* yep
          movea.l   Wdw,a0
          movea.l   wd_UserPort(a0),a0
          movea.l   AbsExecBase,a6
          jsr       _LVOGetMsg(a6)        * get message ptr
          move.l    d0,newmessage
          beq       wfm			* shouldn't be null, but who knows?

* ----------------------------------

          movea.l   d0,a0
          lea       im_message,a1         * save message before replying
          move.w    #11,d4
immover:  move.l    (a0)+,(a1)+
          dbra      d4,immover

* ----------------------------------

          movea.l   newmessage,a1
          move.l    a1,mparms
          move.l    AbsExecBase,a6        * reply
          jsr       _LVOReplyMsg(a6)

* ----------------------------------
* We're only interested in RAWKEY events...

nm:
          move.l    im_class,d7
          subi.w    #RAWKEY,d7
          beq       gkey

          jmp       ddata

* ----------------------------------

gkey:     movea.l   newmessage,a0
          move.w    im_code,d7             * check if rawkey 
          move.w    d7,ie.code
          move.w    im_qualifier,ie.qualifier

* comment out these two lines to see key-up events as well as key-downs
          cmp.w     #$006f,im_code
          bge       wfm

* ----------   convert rawkey
cr:
          lea       ie.event,a0
          lea       ConKey,a1
          clr.l     (a1)
          
          move.l    #0,a2
          move.l    #4,d1

* note how the "base" for RawKeyConvert was obtained from the
* Console Device - a rather peculiar method 
 
          movea.l   ConDbase,a6
          jsr       _LVORawKeyConvert(a6)
          move.b    ConKey,d1
          move.l    ConKey,im_conkey
          move.b    d0,noc 
          jmp       ddata                  * go to display routine

* ------------------ test code
* this qualifier detects the "Amiga-Amiga" combination.
cr2:                         
          cmpi.w    #$80c0,im_qualifier
          beq       close               * press both Amiga to close

kc:       jmp       wfm
         
* ------------------ close everything

close:    clr       d1


cls_wdw:  movea.l   Wdw,a0
          movea.l   IntBase,a6
          jsr       _LVOCloseWindow(a6)

cls_sc:   move.l    CustScr,a0
          movea.l   IntBase,a6
          jsr       _LVOCloseScreen(a6)

cls_glib: movea.l   GfBase,a1
          movea.l   AbsExecBase,a6
          jsr       _LVOCloseLibrary(a6)

cls_dev:  lea       IoRequest,a1
          movea.l   AbsExecBase,a6
          jsr       _LVOCloseDevice(a6)
   
cls_ilib: movea.l   IntBase,a1
          movea.l   AbsExecBase,a6
          jsr       _LVOCloseLibrary(a6)
          move.l    savreturn,a7   

last:     clr.l     d0
          rts

* -------------------- DATA ------------------

               even
dosname:       dc.b  "dos.library",0
               cnop  0,2
IntuitionName: dc.b  "intuition.library",0
               cnop  0,2
GfxName:       dc.b  "graphics.library",0
               cnop  0,2
CName:         dc.b  "console.device",0
               cnop  0,2

* -------------- IoRequest structure

               even
IoRequest:     ds.b    20
device:        ds.b    4
unit:          ds.b    4              
command:       ds.b    2              
flags:         ds.b    1              
error:         ds.b    1              
actual:        ds.b    4              
length:        ds.b    4              
data:          ds.b    4              
offset:        ds.b    4              

* ------------ input event structure

               even
ie.event:      dc.l    0
ie.class:      dc.b    1
ie.subclass:   dc.b    0
ie.code:       ds.w    1
ie.qualifier:  ds.w    1

* ------------- intuition message and rawdofmt parameters

               even
mparms         ds.l    1
im_message:    ds.l    1
               ds.l    1
               ds.l    1
               ds.l    1
               ds.l    1
im_class:      ds.l    1
im_code:       ds.w    1
im_qualifier:  ds.w    1
iaddress:      ds.l    1
im_mousex:     ds.w    1
im_mousey:     ds.w    1
seconds:       ds.l    1
micros:        ds.l    1
               ds.l    1
im_char:       dc.l    ConKey
im_conkey:     ds.l    1

* --------------------- Window Structure

                    even
NewWdw:             dc.w     0
                    dc.w     10
                    dc.w     640
                    dc.w     190
                    dc.b     2
                    dc.b     1
                    dc.l     idcmp1
                    dc.l     Wflags
                    dc.l     0
                    dc.l     0
                    dc.l     Titw
                    dc.l     0
                    dc.l     0
                    dc.w     0
                    dc.w     0
                    dc.w     0
                    dc.w     0
                    dc.w     CUSTOMSCREEN
         
NewCustScreen:
                    dc.w     0
                    dc.w     0
                    dc.w     640
                    dc.w     200
                    dc.w     2
                    dc.b     0
                    dc.b     1
                    dc.w     Modes
                    dc.w     CUSTOMSCREEN
                    dc.l     0
                    dc.l     Tits
                    dc.l     0
                    dc.l     0


            even
Titw:       dc.b    " 100 Henry St, Suite 1229, Brooklyn NY 11201   "
	    dc.b    " (718) 624-5000 x1229",0
Tits:       dc.b    " Console Message Monitor by Maurice St. Sauveur"
	    dc.b    "  Use Amiga-Amiga to Exit",0


* ------------ regs -------------

            even
newmessage: ds.l  1
IntBase:    ds.l  1
dosbase:    ds.l  1
execbase:   ds.l  1
ConDbase:   ds.l  1
GfBase:     ds.l  1
Wdw:        ds.l  1
CustScr:    ds.l  1
Rp:         ds.l  1
Vp:         ds.l  1
savreturn   ds.l  1
fkey:       ds.l  1
code:       ds.w  1
class:      ds.w  1
sptr:       ds.w  1

            even
ConKey:     ds.b  4
            dc.b  0
noc:        dc.b  1
Output:     ds.b  500

* -------------- rawdofmt data routines

          even
Mstring:  dc.b 'GetMsg ptr    $%08lx',$b6
          dc.b '-----------------------',$b6
          dc.b 'ExecMessage   $%08lx',$b6
          dc.b '              $%08lx',$b6
          dc.b '              $%08lx',$b6
          dc.b '              $%08lx',$b6
          dc.b '              $%08lx',$b6
          dc.b 'im_Class      $%08lx',$b6
          dc.b 'im_Code       $%04x',$b6
          dc.b 'im_Qualifier  $%04x',$b6
          dc.b 'Aptr Iaddress $%08lx',$b6
          dc.b 'im_Mousex     $%04x',$b6
          dc.b 'im_Mousey     $%04x',$b6
          dc.b 'seconds       $%08lx',$b6
          dc.b 'micros        $%08lx',$b6
          dc.b 'window ptr    $%08lx',$b6      ,$b6
          dc.b '-----------------------',$b6
          dc.b 'ConKey char    %s    ',$b6
          dc.b 'ConKey hex    $%08lx',$b6,0  

* ------------------ draw data
          even

* Use RawDoFmt to convert internal to (mostly) printable

ddata:
          lea       Mstring,a0
          lea       mparms,a1
          lea       StoreIt,a2
          lea       Output,a3
          move.l    AbsExecBase,a6
          jsr       _LVORawDoFmt(a6)   

          movea.l   GfBase,a6
          move.l    Rp,a1           * set pen to white
          moveq     #1,d0
          jsr       _LVOSetAPen(a6)

* Scan the output string for $B6's and do line - wrap

fl:       lea       Output,a0
          move.w    #20,d1
          movea.l   a0,a4
          clr       d4
fl1:
          cmp.b     #0,1(a4)
          beq       fl5
          cmp.b     #$b6,(a4)
          beq       fl3
fl2:      cmp.b     #0,(a4)
          beq       cr2
          addq.w    #1,d4
          cmp.w     #75,d4
          beq       fl4
          lea       1(a4),a4
          jmp       fl1

* ------------ clean up zeros ---------
* having drawn screen, remove leading zeros.

fl5:      clr       d3
cu:       move.b    noc,d3
          mulu      #2,d3
          clr.l     d5
          move.b    #8,d5
          sub.b     d3,d5    
          clr       d0
          lea       -8(a4),a4
          lea       0(a4,d3.w),a4
          
cz:       cmp.b     #0,noc
          beq       cz2
          cmp.b     #$30,(a4)
          bne       fl6
cz2:      move.b    #$20,(a4)
fl6:      lea       1(a4),a4
          dbra      d5,cz

          jmp       fl7

* --------------------------------
* $B6 found - skip a line


fl3:      lea       1(a4),a4
fl4:      move.w    d1,sptr
          movea.l   GfBase,a6
          move.l    Rp,a1
          move.w    #220,d0
          jsr       _LVOMove(a6)

          move.w    d4,d0       
          movea.l   GfBase,a6
          move.l    Rp,a1
ftext:
          jsr       _LVOText(a6)

          movea.l   a4,a0
          clr       d4
          move.w    sptr,d1
          add.w     #9,d1         
          jmp       fl1

* --------- last parameter -------------------------

fl7:      move.w    d1,sptr
          movea.l   GfBase,a6
          move.l    Rp,a1
          move.w    #220,d0
          jsr       _LVOMove(a6)

          move.w    d4,d0       
          movea.l   GfBase,a6
          move.l    Rp,a1

          jsr       _LVOText(a6)

          movea.l   a4,a0
          clr       d4
          move.w    sptr,d1
          add.w     #8,d1         
          jmp       cr2

* --------- rawkeyconv utility 

StoreIt:  move.b   d0,(a3)+	
          rts

            END

