'===========================================================================
' Subject: ABSOLUTE ASM TO ASIC DECODER       Date: 04-17-99 (16:25)       
'  Author: Dan Autery                         Code: ASIC                   
'  Origin: Autery@aol.com                   Packet: ASIC.ABC
'===========================================================================
REM -- 'ABSOLUTE ASSEMBLY TO ASIC' DECODER v0.9 (beta)
REM -- by Dan Autery (autery@aol.com)
REM
REM ** Created for William Yu's All Basic Code Archives.
REM ** This program is hereby placed in the Public Domain.
REM
REM **  With any program that contains Assembly or Machine Language, you're
REM ** undoubtedly aware that one misplaced or incorrect value can have
REM ** potentially disastrous effects.
REM
REM **  Personally, I can't begin to explain how frustrating it can be to
REM ** have an Assembly routine lock up a program because of some silly
REM ** mistake I've made - either in a HEX to Decimal conversion or because
REM ** I've inadvertantly left out a byte or two from a CODE statement (grr!).
REM ** Naturally, I had to find a solution for this.. ;)  And that's where
REM ** this handy little program comes in. :)
REM
REM **  This utility was created primarily to reduce the potentially
REM ** large amount of work involved in translating the Absolute Assembler's
REM ** HEX codes from QBASIC Call Absolute format to ASIC Decimal CODE
REM ** values.  Perhaps more importantly, though, it also cuts down on the
REM ** number of errors that inevitably pop up during the above process. :]
REM
REM **  I hope you find it helpful.. :>
REM
REM **  CODER.ASI compiles to approximately 5K (as a .COM file) and
REM ** requires no special ASIC settings.  I've tested it pretty extensively
REM ** on ABSASM2.BAS output files, and I've had no problems or glitches
REM ** so far.  However, you should still consider it a BETA (and should
REM ** double-check your output CODE to verify its accuracy until you're
REM ** confident with the program).
REM
REM **  Once compiled, you can activate the program from the DOS prompt
REM ** by typing "coder yourfile.ext" (where 'yourfile.ext' is the name of
REM ** the ABSASM2.BAS output file that you wish to convert).  The utility
REM ** will ask for an output file name in which to place your ASIC CODE.
REM ** Once you've typed in the output file name, the util will do the rest.
REM ** And that's pretty much all there is to it.  Your input file can be
REM ** in a different drive\directory [provided that you include the full
REM ** path to the file in the command line argument], if you wish.

REM *************************  CODE BEGINS HERE ****************************
byt$=COMMAND$
byt$=LTRIM$(byt$)
byt$=RTRIM$(byt$)
IF byt$="" THEN
  COLOR 15,0
  PRINT
  PRINT "                'ABS ASM --> to ASIC Decoder, v0.9'"
  PRINT
  PRINT "    USAGE: 'Coder file.ext'"
  PRINT
  PRINT "    Use this utility on files that have been processed by"
  PRINT "  Petter Holmberg's QBASIC ABSOLUTE ASSEMBLY, v2.0 (ABSASM2.BAS)."
  PRINT
  PRINT "    This program creates a text file containing the ASIC CODE"
  PRINT "  data extracted from the HEX codes created by the above."
  COLOR 7,0
  PRINT
  PRINT "    Coded for the ABC Archives by Dan Autery (autery@aol.com)"
  END
ENDIF
key$=FIND FIRST(byt$,0)
GOSUB errorcheck:
OPEN "R",1,byt$
GOSUB errorcheck:
num=LEN(byt$)
num=num-4
IF num<1 THEN quit:
newname:
 CLS
 LOCATE 1,2
 PRINT "Input file=";
 PRINT byt$
 LOCATE 3,2
 PRINT "Please enter an output file name: ";
 INPUT ofile$
 num=INSTR(ofile$,".")
 IF num<2 THEN quit:
 key$=FIND FIRST(ofile$,0)
 IF ERROR=0 THEN
   PRINT
   PRINT " That file already exists: ";
   IF ofile$<>byt$ THEN
     PRINT "(O)verwrite, ";
   ENDIF
   PRINT "(N)ew name, (C)ancel";
   GOSUB kibit:
   new$=UCASE$(new$)
   IF new$="N" THEN newname:
   IF ofile$=byt$ THEN quit:
   IF new$<>"O" THEN quit:
 ENDIF

OPEN "O",2,"~f1.tmp"
GOSUB errorcheck:

crlf$=CHR$(13)
key$=CHR$(10)
crlf$=crlf$+key$

readmore:
INPUT# 1, byt$ CRLF
IF ERROR=96 THEN write:
IF ERROR>0 THEN newfile:
write:
GOSUB getnum:
GOTO readmore:

newfile:
  CLOSE 1
  CLOSE 2
  OPEN "I",1,"~f1.tmp"
  GOSUB errorcheck:
  OPEN "O",2,ofile$
  GOSUB errorcheck:
  charcount=0

  readnew:
  INPUT# 1, byt$ CRLF
  IF ERROR=96 THEN writenew:
  IF ERROR>0 THEN
    PRINT# 2,crlf$ NONULL
    GOTO quit:
  ENDIF
  writenew:
  IF charcount=0 THEN
    PRINT# 2,crlf$ NONULL
    GOSUB errorcheck:
    PRINT# 2,"CODE " NONULL
    charcount=charcount+5
  ENDIF
  num=LEN(byt$)
  aa=num+charcount
  IF aa>72 THEN
    aa=1
    key$=""
    WHILE key$<>","
      key$=MID$(byt$,aa,1)
      IF aa>num THEN
        key$=","
        aa=1
      ELSE
        IF key$<>"," THEN
          aa=aa+1
        ENDIF
      ENDIF
    WEND
    aa=aa-1
    IF aa>0 THEN
      key$=LEFT$(byt$,aa)
      PRINT# 2,key$ NONULL
      GOSUB errorcheck:
      aa=LEN(key$)
      aa=aa+1
      num=num-aa
      byt$=RIGHT$(byt$,num)
    ENDIF
    charcount=0
    GOTO writenew:
  ENDIF
  charcount=aa
  PRINT# 2,byt$ NONULL
  GOSUB errorcheck:
  GOTO readnew:

quit:
  IF ERROR>0 THEN
    PRINT " An error occurred during the conversion. Please verify your"
    PRINT "source and output files before using."
  ENDIF
  CLOSE 1
  CLOSE 2
  KILL "~f1.tmp"
  END

getnum:
key$=" "
charcount=0
WHILE key$<>""
 key$=""
 tot=LEN(byt$)
 num=INSTR(byt$,"&H")
 IF num<1 THEN
   num=tot
 ELSE
   num=num+1
 ENDIF
 tot=tot-num
 IF tot>0 THEN
   A$=""
   temp2=0
   WHILE A$<>")"
     byt$=RIGHT$(byt$,tot)
     tot=tot-1
     A$=LEFT$(byt$,1)
     IF A$<>")" THEN
       temp1=ASC(A$)
       temp1=temp1-48
       IF temp1>9 THEN
         temp1=temp1-7
       ENDIF
       IF temp2=0 THEN
         temp2=temp1
       ELSE
         temp2=temp2*16
         temp2=temp2+temp1
       ENDIF
     ENDIF
   WEND
   key$=STR$(temp2)
   key$=LTRIM$(key$)
   IF key$="" THEN
     key$="0"
   ENDIF
 ENDIF
 PRINT# 2, key$ NONULL
 GOSUB errorcheck:
 IF key$<>"" THEN
   PRINT# 2, "," NONULL
   GOSUB errorcheck:
   charcount=charcount+1
 ENDIF
WEND
IF charcount>0 THEN
  PRINT# 2, crlf$ NONULL
  GOSUB errorcheck:
ENDIF
RETURN

kibit:
  new$=INKEY$
  IF new$="" THEN kibit:
  RETURN

errorcheck:
  IF ERROR>0 THEN
    GOTO quit:
  ENDIF
RETURN
