*****************************************************************************
* Project Details                                                           *
* ---------------                                                           *
* Project Name:    WBRst                                                    *
* Project Version: 1                                                        *
* Copyright:       Anthony N Peck                                           *
* Date:            Tuesday 23rd May 1995                                    *
*                                                                           *
* Contact:                                                                  *
* 68 Woralul ST                                                             *
* Waramanga ACT 2611                                                        *
* AUSTRALIA                                                                 *
*                                                                           *
*****************************************************************************
*                                                                           *
* File Summary                                                              *
* ------------                                                              *
*                                                                           *
* Usage: WBRst                                                              *
*                                                                           *
* Like it's fellow Rst, it is a small rebooting program.  Unlike Rst, this  *
* one can be run and executed via the Workbench.  Being for Workbench users *
* it also has a safety net if pressed accidentally.  There's a small strip  *
* to move the window if you wish, alternatively you could alter the window  *
* definitions below and re-compile!                                         *
*                                                                           *
* THIS PROGRAM AUTO-DETACHES FROM THE CLI - ALTHOUGH I DIDN'T INCLUDE THE   *
* SOURCE FOR THAT HERE BECAUSE IT ISN'T MINE!                               *
*                                                                           *
*****************************************************************************

*********************************************************************
*                                                                   *
* Some Useful Definitions                                           *
* -----------------------                                           *
*                                                                   *
* You'll find all of these in the various includes that come with   *
* a compiler like DevPac.  Some are defined in structures such as   *
* "GADGET", while others are library offsets.  If you are stuck for *
* such definitions, check out HexTract by Chas Wyndham (Fish 817).  *
*                                                                   *
*********************************************************************

; Simple equivalents

ExecBase                = $04      ; ExecBase (!)
Wd_userport             = $56      ; Window UserPort (Window structure)
Pr_cli                  = $AC      ; CLI offset in process structure
Pr_msgport              = $5C      ; Message port for process structure
Im_class                = $14      ; Class for IDCMP message
Iaddress                = $1C      ; Address of IDCMP message

; Library Vector Offsets

_LVOOpenlibrary         = -$0228   ; Exec function
_LVOOpenwindow          = -$CC     ; Intuition function
_LVOWaitport            = -$0180   ; Exec function
_LVOFindtask            = -$0126   ; Exec function
_LVOForbid              = -$84     ; Exec function
_LVOSetwindowtitles     = -$0114   ; Intuition function
_LVOGetmsg              = -$0174   ; Exec function
_LVOReplymsg            = -$017A   ; Exec function
_LVOAutoRequest         = -$015C   ; Intuition function

*********************************************************************
*                                                                   *
* Some Useful Macros                                                *
* ------------------                                                *
*                                                                   *
* Macros waste memory and increase the size of the code, but they   *
* make life a bit easier and the code a bit more readable.  All of  *
* these are used to access the appropriate library functions.  The  *
* library bases are loaded, the LVO prefix is added, and then the   *
* function is called.  Simple!                                      *
*                                                                   *
*********************************************************************

EXEC            Macro
        MOVE.L  ExecBase,A6        ; Move Exec into a6
        JSR     _LVO\1(A6)         ; Add Library offset and call function
                Endm

CALLINT         Macro
        MOVE.L  _intuitionbase,A6  ; Move Intuition into a6
        JSR     _LVO\1(A6)         ; Add Library offset and call function
                Endm

*********************************************************************
*                                                                   *
* Wb Startup Code                                                   *
* ---------------                                                   *
*                                                                   *
* I modified this slightly from that which is provided with the     *
* includes that come with DevPac.  You can get similiar code        *
* in the Public Domain, see STARTUPS on Fish 101.  All they do is   *
* allow you to start your program from WB.                          *
*                                                                   *
*********************************************************************

        MOVEM.L  D0/A0,-(SP)        ;    save initial values
        CLR.L    Returnmsg          ;    clear message
        SUB.L    A1,A1              ;    subtract address
        EXEC     Findtask           ;    find us
        MOVE.L   D0,A4              ;    move process to a4
        TST.L    Pr_cli(A4)         ;    test if CLI
        BEQ      Fromworkbench      ;    otherwise go to workbench
        MOVEM.L  (SP)+,D0/A0        ;    restore registers
        BRA      End_startup        ;    and run the program

Fromworkbench:

        LEA      Pr_msgport(A4),A0  ;    load the message port
        EXEC     Waitport           ;    wait for a message
        LEA      Pr_msgport(A4),A0  ;    load the message port
        EXEC     Getmsg             ;    then get it
        MOVE.L   D0,Returnmsg       ;    save it for later reply
        MOVEM.L  (SP)+,D0/A0        ;    restore registers

End_startup:

        BSR      Start              ;    call the program
        MOVE.L   D0,-(SP)           ;    save it
        TST.L    Returnmsg          ;    test if message
        BEQ      Exittodos          ;    if I was a CLI
        EXEC     Forbid             ;    forbid WB to close
        MOVE.L   Returnmsg(Pc),A1   ;    load message
        EXEC     Replymsg           ;    message received!

Exittodos:

        MOVE.L   (SP)+,D0           ;    exit code
        RTS

*********************************************************************
*                                                                   *
* Program Starts Here                                               *
* -------------------                                               *
*                                                                   *
* The above code points to this Start identifier, which is where    *
* the actual program starts!                                        *
*                                                                   *
*********************************************************************

Start:

; The opening of the intuition library, including an error branch if things
; don't go according to plan!

        LEA      Intname,A1         ; Load intuition library into a1
        EXEC     Openlibrary        ; Macro opens library
        BEQ      Exittodos          ; If it doesn't open, we quit
        MOVE.L   D0,_intuitionbase  ; Save the return address

; This little bit opens the dinky little window.

        LEA     Win_defs,A0         ; Load window definitions to a0
        CALLINT Openwindow          ; Macro opens window
        MOVE.L  D0,Winhd            ; Save return address (window handle)

; Here the screen title is changed.

        MOVE.L  Winhd,A0            ; Move window handle into a0
        LEA     WinName,A1          ; Move current window title into a1
        LEA     Screentitle,A2      ; Load the screen title into a2
        CALLINT Setwindowtitles     ; Macro changes screen title

Mainloop:

        MOVE.L   Winhd,A0           ; Move window handle into a0
        MOVE.L   Wd_userport(A0),A0 ; UserPort offset at byte 86
        EXEC     Waitport           ; Macro waits for message from window
        MOVE.L   Winhd,A0           ; Move window handle into a0
        MOVE.L   Wd_userport(A0),A0 ; UserPort offset
        EXEC     Getmsg             ; Macro gets window message
        MOVE.L   D0,A1              ; Message transferred to a1
        EXEC     Replymsg           ; Message received!

        MOVE.L   Winhd,A0           ; Load the window handle to A0...
        LEA      Atext,A1           ; ...and the main body text to A1...
        LEA      Ltext,A2           ; ...and the left text to A2...
        LEA      Rtext,A3           ; ...and the right text to A3
        MOVE.L   #$00,D0            ; left button responds to mouse click
        MOVE.L   #$00,D1            ; right button responds to mouse click
        MOVE.L   #$C4,D2            ; requester length
        MOVE.L   #$35,D3            ; requester height
        CALLINT  AutoRequest        ; call the requester
        TST.B    D0                 ; Test if clicked the right button
        BNE      Mainloop           ; No...so back for another message

Bang:

; Clear the vectors and reboot

        MOVE.L  $04,A6              ; ExecBase to A6
        CLR.L   $32(A6)             ; Clear Warm Capture
        CLR.L   $2E(A6)             ; Clear Cool Capture
        CLR.L   $2A(A6)             ; Clear Cold Capture
        CLR.L   $226(A6)            ; Clear KickTagPtr

        LEA     Boot,A5             ; Load boot routine to A5
        JSR     -$1E(A6)            ; Supervisor mode reboot

Boot:   LEA     $F000000,A0         ; Load silly memory location to A0
        RESET                       ; Stack pointer for supervisor mode
        JMP     (A0)                ; Jump and gone...

*********************************************************************
*                                                                   *
* Structures and Memory Allocation                                  *
* --------------------------------                                  *
*                                                                   *
* Here are the definitions held for the structures used in the      *
* program, as well as memory blocks set aside for definitions such  *
* as the messages.                                                  *
*                                                                   *
*********************************************************************

Win_defs:

; Window structure (some help here from FASTWINDOWS by R Plant)

        DC.W $226                   ; Wdw X    | Alter position of window
        DC.W $00                    ; Wdw Y    | using these two values
        DC.W $19                    ; Wdw W
        DC.W $B                     ; Wdw H
        DC.B $01                    ; Col 0
        DC.B $00                    ; Col 1
        DC.L $00000200              ; IDCMP
        DC.L $0000000A              ; TYPE
        DC.L $00                    ; No Gadgets
        DC.L $00                    ; Standard Checkmark
        DC.L WinName                ; Window name 
        DC.L $00                    ; screen pointer
        DC.L $00                    ; no custom bitmap
        DC.W $00,$00,$00,$00        ; intuition sets these given that...
        DC.W $01                    ; this is a WORKBENCH screen 

Atext:

; Intuitext structure

        DC.B    $02,$01,$01         ; FrontPen,BackPen,DrawMode

        Even                        ; Fill to make even address for word

        DC.W    $0D,$08             ; Left edge, top edge
        DC.L    $00                 ; No special font
        DC.L    Atx                 ; The actual text
        DC.L    $00                 ; No next text 

Ltext:

; Intuitext structure

        DC.B    $02,$01,$01         ; FrontPen,BackPen,DrawMode

        Even                        ; Fill to make even address for word

        DC.W    $06,$03             ; Left edge, top edge
        DC.L    $00                 ; No special font
        DC.L    Ltx                 ; The actual text
        DC.L    $00                 ; No next text 

Rtext:

; Intuitext structure

        DC.B    $02,$01,$01         ; FrontPen,BackPen,DrawMode

        Even                        ; Fill to make even address for word

        DC.W    $06,$03             ; Left edge, top edge
        DC.L    $00                 ; No special font
        DC.L    Rtx                 ; The actual text
        DC.L    $00                 ; No next text 

_intuitionbase: DS.L    $01         ; memory for intuition base
Returnmsg:      DS.L    $01         ; memory for WB return message
Winhd:          DS.L    $01         ; memory for window handle
Atx:            DC.B    "Reboot?",$00
Ltx:            DC.B    "No",$00
Rtx:            DC.B    "Yes",$00
WinName:        DC.B    "   Your Choice",$00
Screentitle:    DC.B    "FREEWARE - A N Peck (1995)",$00
Intname:        DC.B    "intuition.library",$00  ; intuition library name

                End     ; Adios Amigas


