;
;      PAL/NTSC Switch    by Ed Mackey
;       "on the fly"         440 Louella Ave.
;                            Wayne, PA 19087

;  This program is supposed to let you change between PAL and NTSC
;  modes after your computer is already booted up.  Unfortunately,
;  the length of the WorkBench screen is not affected, but the display
;  (number of scan lines) and timers are!

;  You must have a "Fatter" Agnus for this program to work.  If you
;  run this program and nothing happens, you probably only have the
;  "Fat" Agnus.  If you KNOW you have the "Fatter" Agnus and this
;  program STILL doesn't do anything, well, you got your money's
;  worth for a free program.  I hope this happens to nobody, and I
;  don't know why it would.

;  I'm sorry this program isn't a Super-Flexible-User-Friendly
;  work of organized programming, but it was made in all of
;  15 minutes.   ;->

;  Here is the source code, assembled with Assempro by Abacus.
;  This is Public Domain.  Please don't change the credits, though.

       Include 'includes:IncludeMe'
       Include 'includes:intuition.offsets'

       ILABEL 'Includes:Amiga.L'

       INIT_AMIGA   ;Run me from WB!

       bsr     run

       EXIT_AMIGA

; Special thanks to Andy Hook for providing the following address
; in the source code to his PD version of Tetris:

PalSwitch = $DFF1DC    ;Usage:  MOVE.B #NTSC,PALSWITCH
NTSC      = 2          ;   or:  MOVE.B #PAL ,PALSWITCH
PAL       = 32

; This program would not exist without it!  Thanks!

; (AmigaBASIC version of the above:
;    PalSwitch& = 14676444
;    NTSC% = 2 : PAL% = 32
;    POKE PALSWITCH&,PAL%    'For PAL
;    POKE PALSWITCH&,NTSC%   'For NTSC (duh...)
;  but do NOT ever PEEK(PALSWITCH&) unless you want to see funny
;  lines that force you to reboot!)

run:
       bsr     openint      ;open intuition.library
       beq     IntKicked    ;uh-oh.  Where's intuition?!?!
       bsr     windopen     ;make my window.
       beq     WindKicked
       bsr     changetitle
       move.b  #NTSC,CurrentMode
       move.b  CurrentMode,PalSwitch   ;At start, default to NTSC.
       bsr     TellUser
loop:
       move.l  EBase,a6
       move.l  windowhd,a0
       move.l  86(a0),a0
       jsr     _LVOWaitPort(a6)  ;wait for a gadget.
       move.l  #0,d7
loop2:
       move.l  windowhd,a0
       move.l  86(a0),a0
       jsr     _LVOGetMsg(a6)    ;read in the gadget message.
       tst.l   d0
       bne     ReadMsg           ;decode it
       cmp.l   #0,d7
       beq     loop              ;no message?
       cmp.l   #2,d7
       beq     GoForIt           ;Custom gadget: Switch modes!
ende:
       bsr     windclose         ;Close Gadget: Remove window & exit
WindKicked:
       bsr     closeint
IntKicked:
       rts   ;that's all folks!
ReadMsg:
       move.l  d0,a5
       move.l  20(a5),d6
       move.l  d6,d0             ;Decode this IDCMP message.
       and.l   #$200,d0          ;Was it the Close gadget?
       cmp.l   #0,d0
       beq     NotClose          ;Nope.
       move.l  #1,d7
       bra     Reply
NotClose:
       move.l  d6,d0
       and.l   #$40,d0           ;How 'bout MY gadget?
       tst.l   d0
       beq     Reply
       move.l  28(a5),d0         ;Aha!!  Here it is!
       cmp.l   #gadget1,d0       ;Check WHICH custom gadget it was.
       bne     Reply             ;(this is obviously not needed in
       move.l  #2,d7             ;this case, with only one custom gad,
Reply:                           ;but it's good planning ahead!)
       move.l  a5,a1
       move.l  EBase,a6
       jsr     _LVOReplyMsg(a6)  ;ReplyMsg() to ALL messages!
       bra     Loop2
GoForIt:                      ;The actual switcheroo!
       clr     d0
       move.b  #NTSC+PAL,d0   ;Put their sum in d0...
       clr     d1
       move.b  CurrentMode,d1
       sub     d1,d0          ;...then subtract one to get the other!
       move.b  d0,CurrentMode
       move.b  d0,PalSwitch   ;Send it to the hardware...
       bsr     TellUser
       bra     Loop
TellUser:                     ;Tell user what mode he's in.
       move.l  windowhd,a0
       move.l  50(a0),a0      ;find RastPort
       clr.l   d0
       clr.l   d1
       lea     NTSCtext,a1
       cmp.b   #NTSC,CurrentMode
       beq     Ok
       lea     PALtext,a1
Ok:    move.l  intbase,a6
       jsr     _LVOPrintIText(a6)  ;output text.
       rts
openint:
       move.l  EBase,a6
       lea     intname,a1
       move.l  #0,d0
       jsr     _LVOopenlibrary(a6)  ;open intuition.library
       move.l  d0,intbase
       tst.l   d0
       rts
closeint:
       move.l  EBase,a6
       move.l  intbase,a1
       jsr     _LVOcloselibrary(a6) ;close intuition.library
       rts
windopen:
       move.l  intbase,a6
       lea     windowdef,a0
       jsr     _LVOopenwindow(a6)   ;make my window.
       move.l  d0,windowhd
       tst.l   d0
       rts
changetitle:
       move.l  d0,a0
       lea     windname,a1
       lea     tite1,a2
       jsr     _LVOSetWindowTitles(a6)  ;Change Screen title bar.
       rts
windclose:
       move.l  intbase,a6
       move.l  windowhd,a0
       jsr     _LVOclosewindow(a6)  ;close my window.
       rts

       DATA   ;data section!
windowdef:
       dc.w    100,50,142,30
       dc.b    3,1
       dc.l    $240,$100e,gadget1,0,windname
       dc.l    0,0
       dc.w    0,0,0,0,1
tite1:
       dc.b    'PAL/NTSC Switch "on the fly"     by Ed Mackey        :-)',0
       align
windname:
       dc.b    'FASTPAL',0
       align
gadget1:
       dc.l    0
       dc.w    75,14,57,11,0,1,1
       dc.l    boarder,0,gotext,0,0
       dc.w    8  ;ID
       dc.l    0
gotext:
       dc.b    1,0,1,0
       dc.w    1,2  ;x,y
       dc.l    0,goascii,0
NTSCtext:
       dc.b    1,0,1,0
       dc.w    10,16
       dc.l    0,NTSCascii,0
PALtext:
       dc.b    3,0,1,0
       dc.w    10,16
       dc.l    0,PALascii,0
goascii:
       dc.b    'CHANGE!',0
       align
NTSCascii:
       dc.b    'NTSC',0
       align
PALascii:
       dc.b    'PAL ',0
       align
boarder:
       dc.w    0,0
       dc.b    3,0,0,5
       dc.l    ptlist,0
ptlist:
       dc.w    -1,-1,  57,-1,  57,11,  -1,11,  -1,-1
intname:
       dc.b    'intuition.library',0
       align
       BSS
windowhd:
       ds.l    1
CurrentMode:
       ds.w    1
intbase:
       ds.l    1
       END
