/*rx
 *
 * UAEWB.aux - auxiliary rx script for UAEWB
 *
 * $VER: UAEWB.aux 0.4 (01.12.04) © by Samuel Devulder
 *
 * (c) 26.01.98 Samuel Devulder
 *     01.12.04 Richard Drummond
 *
 * Usage: UAEWB.aux [a500|ham|default|about|use|save]
 */

Cycle_Active            = 0x80421788
Selected                = 0x8042654B
Slider_Level            = 0x8042AE3A
MUIA_Background         = 0x8042545b
MUIA_ControlChar        = 0x8042120b
MUIA_Frame              = 0x8042ac64
MUIA_Text_HiChar        = 0x804218ff
MUIA_Window_DepthGadget = 0x80421923
MUIA_Window_DragBar     = 0x8042045d
MUIA_Window_SizeGadget  = 0x8042e33d

MUII_WindowBack  = 0
MUIV_Frame_None  = 0
FALSE = 0

OPTIONS RESULTS
PARSE ARG ACTION .
ACTION=upper(ACTION)

ADDRESS UAEWB

UAEPREFS = "ENV:UAEWB"

/* if set prefs to default */
IF ACTION='DEFAULT' THEN DO
  CYCLE  ID SIZE LABEL "LORES"
  CYCLE  ID SCRN LABEL "PUBLIC"
  STRING ID SM CONTENT ""
  CYCLE  ID DEPTH LABEL "N/A"
  SLIDER ID SLDR ATTRS 0x8042AE3A  "1"
  CHECK  ID GRAY ATTRS 0x8042654B  "0"
  CHECK  ID DITH ATTRS 0x8042654B  "1"
  CYCLE  ID CHIP LABEL "2 Mb"
  CYCLE  ID SLOW LABEL "None"
  CYCLE  ID FAST LABEL "None"
  POPSLIDER ID Z3RAM ATTRS 0x8042AE3A  "0"
  POPASL ID ROMF CONTENT ""
  POPASL ID KEYF CONTENT ""
  POPASL ID DF0 CONTENT ""
  POPASL ID DF1 CONTENT ""
  POPASL ID DF2 CONTENT ""
  POPASL ID DF3 CONTENT ""
  STRING ID VMOU1 CONTENT ""
  STRING ID VMOU2 CONTENT ""
  POPASL ID VPAT1 CONTENT ""
  POPASL ID VPAT2 CONTENT ""
  CYCLE  ID SOUND LABEL "NONE"
  CYCLE  ID AUDBIT LABEL "8 bits"
  CYCLE  ID AUDMS LABEL "mono"
  POPSLIDER ID FREQ ATTRS 0x8042AE3A  "11"
  POPSLIDER ID BUFF ATTRS 0x8042AE3A  "8"
  POPASL ID SFILE CONTENT ""
  CYCLE  ID FBLT LABEL "ON"
  CYCLE  ID GUI LABEL "Enabled"
  CYCLE  ID JOY0 LABEL "Mouse"
  CYCLE  ID JOY1 LABEL "Joystick"
  CYCLE  ID CPUT LABEL "68020"
  SLIDER ID CPUR ATTRS 0x8042AE3A "4"
  POPASL ID SIFF CONTENT ""
  STRING ID EXTRA CONTENT ""
  EXIT 0
END; ELSE IF ACTION='A500' THEN DO
  CYCLE  ID CHIP LABEL "512 Kb"
  CYCLE  ID SLOW LABEL "512 Kb"
  CYCLE  ID FAST LABEL "None"
  POPSLIDER ID Z3RAM ATTRS 0x8042AE3A  "0"
  CYCLE  ID CPUT LABEL "68000"
  SLIDER ID CPUR ATTRS 0x8042AE3A "4"
  EXIT 0
END; ELSE IF ACTION='HAM' THEN DO
  STRING ID SM CONTENT "0x21800"
  CYCLE  ID DEPTH LABEL "6"
  CYCLE  ID SCRN LABEL "ModeID"
  EXIT 0
END; ELSE IF ACTION='ABOUT' THEN DO
  WINDOW ID ABOUT ATTRS MUIA_Window_DepthGadget FALSE,
                  MUIA_Window_DragBar FALSE MUIA_Window_SizeGadget FALSE
    GROUP HORIZ
      BUTTON ICON "UAEWB" TRANS ATTRS MUIA_Background MUII_WindowBack,
                                      MUIA_Frame MUIV_Frame_None
      TEXT ATTRS MUIA_Background MUII_WindowBack MUIA_Frame MUIV_Frame_None,
           LABEL "\033cUAE is the UAE Amiga Emulator\n"||,
                 "(\033irecursive definition\033n)\n"||,
                 "by Bernd Schmidt & contributors\n\n"||,
                 "Amiga Port by Samuel Devulder\n\n"||,
                 "This GUI uses MUIRexx\nby Russell Leighton"
    ENDGROUP
    GROUP HORIZ
      SPACE HORIZ
      BUTTON ID OK_BUTT COMMAND """WINDOW ID ABOUT CLOSE""" PORT UAEWB ATTRS MUIA_ControlChar 13 LABEL "Ok"
      SPACE HORIZ
    ENDGROUP
  ENDWINDOW
  EXIT 0
END

/* ELSE use or save */
WINDOW ID WAIT ATTRS 0x80421923 0 0x8042045d 0
  LABEL "\033b\033cPlease wait\nwhile UAE is booting...\033n"
ENDWINDOW

IF ~OPEN(ENVFILE,UAEPREFS,"w") then exit(255)
call writeln(ENVFILE,"/*rx*/");
call writeln(ENVFILE,"ADDRESS UAEWB")

/* DISPLAY */
UAE_SIZE  = get_val("CYCLE  ID SIZE",LABEL)
UAE_SCRN  = get_val("CYCLE  ID SCRN",LABEL)
UAE_SM    = get_val("STRING ID SM",CONTENT)
UAE_DEPTH = get_val("CYCLE  ID DEPTH",LABEL)
UAE_FRATE = get_val("SLIDER ID SLDR ATTRS" Slider_Level,"")
UAE_GRAY  = get_val("CHECK  ID GRAY ATTRS" Selected,"")
UAE_DITH  = get_val("CHECK  ID DITH ATTRS" Selected,"")

/* MEMORY */
UAE_CHIP  = get_val("CYCLE ID CHIP",LABEL)
UAE_SLOW  = get_val("CYCLE ID SLOW",LABEL)
UAE_FAST  = get_val("CYCLE ID FAST",LABEL)
UAE_Z3    = get_val("POPSLIDER ID Z3RAM ATTRS" Slider_Level,"")
UAE_ROM   = get_val("POPASL ID ROMF",CONTENT)
UAE_KEY   = get_val("POPASL ID KEYF",CONTENT)

/* DISKS */
UAE_DF0   = get_val("POPASL ID DF0",CONTENT)
UAE_DF1   = get_val("POPASL ID DF1",CONTENT)
UAE_DF2   = get_val("POPASL ID DF2",CONTENT)
UAE_DF3   = get_val("POPASL ID DF3",CONTENT)
UAE_VMOU1 = get_val("STRING ID VMOU1",CONTENT)
UAE_VMOU2 = get_val("STRING ID VMOU2",CONTENT)
UAE_VPAT1 = get_val("POPASL ID VPAT1",CONTENT)
UAE_VPAT2 = get_val("POPASL ID VPAT2",CONTENT)

/* SOUND */
UAE_SOUND = get_val("CYCLE ID SOUND",LABEL)
UAE_AUDMS = get_val("CYCLE ID AUDMS",LABEL)
UAE_AUDBI = get_val("CYCLE ID AUDBIT",LABEL)
UAE_FREQ  = get_val("POPSLIDER ID FREQ ATTRS" Slider_Level,"")
UAE_BUFF  = get_val("POPSLIDER ID BUFF ATTRS" Slider_Level,"")
UAE_SFILE = get_val("POPASL ID SFILE",CONTENT)

/* MISC */
UAE_FBLT  = get_val("CYCLE ID FBLT",LABEL)
UAE_GUI   = get_val("CYCLE ID GUI",LABEL)
UAE_JOY0  = get_val("CYCLE ID JOY0",LABEL)
UAE_JOY1  = get_val("CYCLE ID JOY1",LABEL)
UAE_CPUT  = get_val("CYCLE ID CPUT",LABEL)
UAE_CPUR  = get_val("SLIDER ID CPUR ATTRS" Slider_Level,"")
UAE_SIFF  = get_val("POPASL ID SIFF",CONTENT)
UAE_EXTRA = get_val("STRING ID EXTRA",CONTENT)

call CLOSE(ENVFILE)

/* IF SAVE prefs to envarc, do it */
IF ACTION='SAVE' THEN ADDRESS COMMAND "COPY "UAEPREFS" ENVARC: NOREQ QUIET"

/* create UAE command line */
ADDRESS COMMAND
line = ""

/* video options */
UAE_SIZE = upper(UAE_SIZE)
UAE_SCRN = upper(UAE_SCRN)
IF UAE_SIZE = 'LORES' then line = line "-O320:256:XYl"
IF UAE_SIZE = 'HIRES' then line = line "-O640:512:XYd"
IF UAE_SCRN = 'MODEID' then DO
    IF (UAE_DEPTH = "N/A") | (UAE_SM=="") | (UAE_SM=0) THEN DO
      'RequestChoice "UAELauncher - Error" "Please set ModeID and Depth correctly" "Ok!"'
      EXIT 0
    END
    line = line "-H2"
    Delete "ENV:UAESM >NIL:"
    Avail flush ">NIL:"
    SetEnv UAESM UAE_SM":"UAE_DEPTH
END
IF UAE_SCRN = ASLREQ then line = line "-H2"
IF UAE_SCRN = PUBLIC then line = line "-H1"
IF UAE_SCRN = DEFSCR then line = line "-H0"
line = line "-R"UAE_FRATE
IF  UAE_GRAY then line = line "-T"
IF ~UAE_DITH then line = line "-x"

/* RAM options */
PARSE VAR UAE_CHIP NUM .;IF NUM<512 THEN NUM=NUM*1024;NUM=NUM/512;
                         IF NUM~=4  THEN line = line "-c"NUM
PARSE VAR UAE_SLOW NUM .;IF NUM=512  THEN line=line "-s2"
                         IF NUM=1024 THEN line=line "-s4"
PARSE VAR UAE_FAST NUM .;IF upper(NUM)~='NONE' THEN line=line "-F"NUM
PARSE VAR UAE_Z3   NUM .;IF NUM~=0 THEN line=line "-Z"NUM
IF OPEN(ROM,UAE_ROM,'R') THEN DO
  line=line "-r"UAE_ROM;
  call close(ROM);
end
IF OPEN(ROM,UAE_KEY,'R') THEN DO
  line=line "-K"UAE_KEY;
  call close(ROM);
end

/* DISKS */
UAE_VMOU1=compress(UAE_VMOU1,":")
UAE_VMOU2=compress(UAE_VMOU2,":")
IF UAE_DF0~="" THEN line=line "-0"UAE_DF0
IF UAE_DF1~="" THEN line=line "-1"UAE_DF1
IF UAE_DF2~="" THEN line=line "-2"UAE_DF2
IF UAE_DF3~="" THEN line=line "-3"UAE_DF3
IF UAE_VMOU1~="" & UAE_VPAT1~="" THEN line=line "-m"UAE_VMOU1":"UAE_VPAT1
IF UAE_VMOU2~="" & UAE_VPAT2~="" THEN line=line "-m"UAE_VMOU2":"UAE_VPAT2

/* SOUND */
UAE_SOUND=upper(UAE_SOUND)
UAE_AUDMS=upper(UAE_AUDMS)
UAE_AUDBI=upper(UAE_AUDBI)
IF      UAE_SOUND="NONE" THEN line=line "-S0"
ELSE IF UAE_SOUND="OFF"  THEN line=line "-S1"
ELSE DO
  IF UAE_SOUND="ON" THEN line=line "-S2"; ELSE line=line "-S3"
  IF UAE_AUDMS="MONO" THEN line=line":m"; ELSE line=line":s"
  IF UAE_AUDBI="8 BITS" THEN line=line":8"; ELSE line=line":16"
  line=line":"||trunc(UAE_FREQ*11025/11)||":"UAE_BUFF*1024
END
IF OPEN(ZZZ,UAE_SFILE,"w") THEN DO
  CALL CLOSE(ZZZ)
  ADDRESS COMMAND "Delete >NIL: ENV:AUDIONAME"
  ADDRESS COMMAND 'Setenv AUDIONAME "'UAE_SFILE'"'
END

/* MISC */
UAE_CPUT=upper(UAE_CPUT)
IF upper(UAE_FBLT)="ON"    THEN line=line "-n3i"
IF      UAE_CPUT="MC68K"   THEN line=line "-C0c"
ELSE IF UAE_CPUT="68000"   THEN line=line "-C0"
ELSE IF UAE_CPUT="68010"   THEN line=line "-C1"
ELSE IF UAE_CPUT="68020"   THEN line=line "-C2"
ELSE IF UAE_CPUT="020/881" THEN line=line "-C3"
IF UAE_CPUR~=4  THEN line=line "-w"||UAE_CPUR
IF UAE_SIFF~="" THEN DO
  ADDRESS COMMAND 'Delete >NIL: ENV:UAEIFF'
  ADDRESS COMMAND 'Setenv UAEIFF "'UAE_SIFF'"'
END
UAE_JOY0=upper(UAE_JOY0)
UAE_JOY1=upper(UAE_JOY1)
IF UAE_JOY0="MOUSE"    THEN ZZZ="-JM"
IF UAE_JOY0="JOYSTICK" THEN ZZZ="-J1"
IF UAE_JOY0="KEYPAD"   THEN ZZZ="-Ja"
IF UAE_JOY0="ARROWS"   THEN ZZZ="-Jb"
IF UAE_JOY1="MOUSE"    THEN ZZZ=ZZZ||"M"
IF UAE_JOY1="JOYSTICK" THEN ZZZ=ZZZ||"1"
IF UAE_JOY1="KEYPAD"   THEN ZZZ=ZZZ||"a"
IF UAE_JOY1="ARROWS"   THEN ZZZ=ZZZ||"b"
IF ZZZ~="-JM1" THEN line=line ZZZ

IF UAE_EXTRA~="" THEN line=line UAE_EXTRA

/* GUI ? */
IF upper(UAE_GUI)="ENABLED" THEN DO
   IF EXISTS("src/UAEGUI.rexx") THEN
      ADDRESS COMMAND "Run >NIL: rx src/UAEGUI.rexx"
   ELSE IF EXISTS("amiga/UAEGUI.rexx") THEN
      ADDRESS COMMAND "Run >NIL: rx amiga/UAEGUI.rexx"
   ELSE ADDRESS COMMAND "Run >NIL: rx UAEGUI.rexx"
END

/* and run UAE */
ADDRESS UAEWB 'QUIT'
ADDRESS COMMAND
TMP='t:UAEWB.'time('s')
'echo  >'TMP' "FailAt 21"'
'echo >>'TMP' "version >NIL: ppc.library"'
'echo >>'TMP' "IF NOT ERROR"'
'echo >>'TMP' "  Set UAE uae.elf"'
'echo >>'TMP' "ELSE"'
'echo >>'TMP' "  Set UAE uae"'
'echo >>'TMP' "ENDIF"'
'echo >>'TMP' "echo Starting ''*E[1m$UAE'line'*E[0m''..."'
'echo >>'TMP' "Stack 32000"'
'echo >>'TMP' "$UAE'line'"'
'echo >>'TMP' "echo Exiting..."'
'echo >>'TMP' "Wait 10"'
'echo >>'TMP' "run >NIL: WAIT 5 +"'
'echo >>'TMP' "Delete 'TMP'"'
'echo >>'TMP' "Endcli"'
'Newcli "con:/160///UAE - Output window (Use GUI or CTRL-C to quit)/NOCLOSE" from 'TMP
exit(0)

/* GET value and update prefs */
get_val: PROCEDURE
  PARSE ARG CMD, SET
  address UAEWB CMD
  GVAL=RESULT
  call writeln(ENVFILE,CMD SET '"'||GVAL'"')
return GVAL
