$EVENT OFF %TRUE = -1 %FALSE = 0 print Open "Complete.Txt" For Output as #2 Gosub AddCopyrightNotice Gosub ReadKnown Gosub ReadImage Gosub DisplayMapping Gosub DecodeImage Close#2 System REM ************************************************************************** REM * * REM * REM * * REM ************************************************************************** Dim opc$(&h3FF) ReadKnown: For i& = 0 to &h3FF opc$(i&) = "" Next Select Case Ucase$(Left$(Trim$(Command$),4)) Case "" Call LoadFrom("Guessed.Txt") Call LoadFrom("Known.Txt") Case "GUES" Call LoadFrom("Guessed.Txt") Case "NONE End Select For i& = 0 to &h3FF opc$(i&) = RemoveSpace$(opc$(i&)) Next Return Sub LoadFrom(f$) Shared opc$() Local l$ Local pc& Local i& local o$ If dir$(f$)<>"" Then Open f$ for input as #1 While Not Eof(1) Line Input#1,l$ l$ = Trim$(l$) If left$(l$,1)<>"'" Then ' 123 456789-1 2 ' nnn 12345678 nnn l$ = RemoveSpace$(l$) pc& = Val("&h"+Left$(l$,3)) l$ = Mid$(l$,12,32) o$ = opc$(pc&) If len(o$) = 0 Then opc$(pc&) = l$ else for i& = 1 to 32 If mid$(l$,i&,1) <> "-" Then mid$(o$,i&,1) = mid$(l$,i&,1) End If next opc$(pc&) = o$ End If End If wend Close#1 End If End Sub REM ************************************************************************** REM * * REM * REM * * REM ************************************************************************** Dim mapFrom$(32) ReadImage: For i& = 1 to 32 ' opcode zcri cccc ddddddddd ssssssssss mapFrom$(i&) = RemoveSpace$("FEDCBA zyxw vuts rqponmlkj ihgfedcba") Next pc& = 0 Open "CogImage.Txt" For Input As #1 While Not Eof(1) Line input#1, l$ If pc& < &h1F0 or ( pc& >= &h200 and pc& < &h3F0 ) Then If Len(opc$(pc&)) <> 0 Then l$ = Trim$(Right$(l$,Len(l$)-3)) b$ = ConvertHexToBit$(l$) Call SetMapping(b$,opc$(pc&)) End If End If pc& = pc&+1 Wend Close#1 Return REM ************************************************************************** REM * * REM * REM * * REM ************************************************************************** DecodeImage: pc& = 0 Open "CogImage.Txt" For Input As #1 Open "Known.Txt" For Output as #3 While Not Eof(1) Line input#1, l$ If pc& < &h1F0 or ( pc& >= &h200 and pc& < &h3F0 ) Then Gosub DescribeSection l$ = Trim$(Right$(l$,Len(l$)-3)) print#2, FNhex$(pc& and &h1FF,3)+" "+l$; b$ = ConvertHexToBit$(l$) o$ = DecodeInstruction$(b$) If instr(o$,"-") > 0 Then bitSet0& = 0 bitSet1& = 0 For j& = 1 to 32 Select Case mid$(b$,j&,1) Case "0" : bitSet0& = bitSet0&+1 Case "1" : bitSet1& = bitSet1&+1 End Select next For j& = 1 to 32 Select Case mid$(o$,j&,1) Case "0" : bitSet0& = bitSet0&-1 Case "1" : bitSet1& = bitSet1&-1 End Select next If bitSet0&+bitSet1& = 1 Then for j& = 1 to 32 If mid$(o$,j&,1) = "-" Then mid$(o$,j&) = hex$(bitSet1&) End If next End If End If opc$ = mid$(o$,1,6) zcr$ = mid$(o$,7,4) cnd$ = mid$(o$,11,4) dst$ = mid$(o$,15,9) src$ = mid$(o$,24,9) print#2," "+opc$+" "+zcr$+" "+cnd$+" "+dst$+" "+src$+" "; Gosub DisAssemble End If pc& = pc&+1 Wend Close#1 Close#3 print#2,"" print#2, "Fully decoded";decoded&;"and partially decoded";partial& print#2,"" print print "Fully decoded";decoded&;"and partially decoded";partial& Return DescribeSection: o$ = "" Select Case pc& Case &h008 : o$ = "Bytecode Fetch, Dispatch and Execute" Case &h01F : o$ = "Push Stack Frame ?" Case &h0B6 : o$ = "Return from Subroutine / Abort ?" Case &h0C7 : o$ = "Push Constant - Packed Byte" Case &h0CF : o$ = "Push Constant - 8, 16, 24 or 32-bit" Case &h158 : o$ = "Push Long to Stack" Case &h19B : o$ = "Randomise a Long" Case &h1A4 : o$ = "Sign Extend Byte or Long" Case &h1D8 : o$ = "Pop Arguments From Stack" Case &h1DF : o$ = "Swap Limits and Flag If Target is in Range" Case &h1E5 : o$ = "Constants" Case &h1E8 : o$ = "Run-Time Variables" End Select if o$ <> "" Then If not hadCrLf Then print#2,"" End If print#2,tab(53);o$ print#2,"" hadCrLf = %TRUE End If Return DisAssemble: If instr(o$,"-") = 0 Then decoded& = decoded&+1 else If instr(left$(o$,6),"-") = 0 Then partial& = partial&+1 End If End If If pc& >= &h1E5 Then Select Case pc& Case &h1EB : print#2,"pcBase"; Case &h1EC : print#2,"vpBase"; Case &h1ED : print#2,"spBase"; Case &h1EE : print#2,"pc"; Case &h1EF : print#2,"sp"; End Select print#2,tab(66);"LONG $"+ConvertBitToHex$(o$) hadCrLf = %FALSE else Select Case pc& Case &h008 : print#2,"Fetch "; Case &h019 : print#2,"JmpVector "; Case &h158 : print#2,"PushLong "; Case &h1D8 : print#2,"Pop3 "; Case &h1DA : print#2,"Pop2 "; Case &h1DC : print#2,"Pop1 "; Case Else : print#2,FNifs$(cnd$); End Select opx$ = FNopc$(opc$) arg$ = "" i& = 1 while i& <= len(opx$) Select Case mid$(opx$,i&,1) Case "d" : arg$ = arg$+ FNreg$(0,dst$) Case "s" : Select Case mid$(zcr$,4,1) Case "0" Case "1" : arg$ = arg$+"#" Case Else : arg$ = arg$+"{#}" End Select arg$ = arg$+FNreg$(-1,src$) Case "c" : arg$ = arg$+FNreg$(0,dst$) Case "j","m" : arg$=arg$+FNreg$(0,src$) Case "r" If src$ = "000001000" Then arg$ = arg$+"Fetch" else arg$ = arg$+FNreg$(0,src$) End If Case "+" i& = i&+1 Select Case mid$(opx$,i&,1) Case "z" Select Case mid$(zcr$,1,1) Case "0" : Case "1" : arg$ = arg$+" WZ" Case Else : arg$ = arg$+" {WZ?}" End Select Case "c" Select Case mid$(zcr$,2,1) Case "0" : Case "1" : arg$ = arg$+" WC" Case Else : arg$ = arg$+" {WC?}" End Select Case "r" Select Case mid$(zcr$,3,1) Case "0" : Case "1" : arg$ = arg$+" WR" Case Else : arg$ = arg$+" {WR?}" End Select Case Else : arg$ = arg$+"+"+mid$(opx$,i&,1) End Select Case "-" i& = i&+1 Select Case mid$(opx$,i&,1) Case "r" Select Case mid$(zcr$,3,1) Case "0" : arg$ = arg$+" NR" Case "1" : Case Else : arg$ = arg$+" {WR?}" End Select Case Else : arg$ = arg$+"-"+mid$(opx$,i&,1) End Select Case Else : arg$ = arg$+mid$(opx$,i&,1) End Select i& = i&+1 wend Select Case left$(opx$,4) Case "JMPR" Select Case right$(arg$,10) Case "$1DE,#$1D8" : arg$ = "CALL #Pop3" Case "$1DE,#$1DA" : arg$ = "CALL #Pop2" Case "$1DE,#$1DC" : arg$ = "CALL #Pop1" End Select Case else Select Case right$(arg$,5) Case ",$085" : arg$ = arg$+" ( $0000_FFFF )" Case ",$138" : arg$ = arg$+" ( $0000_FFFC )" Case ",$1E5" : arg$ = arg$+" ( $FFFF_FFFF )" Case ",$1E6" : arg$ = arg$+" ( $8000_0000 )" Case ",$1E7" : arg$ = arg$+" ( $0080_0000 )" End Select End Select Select Case pc& Case &h019 : arg$ = "JMP #0-0" Case &h085 : arg$ = "LONG $"+ConvertBitToHex$(o$) Case &h138 : arg$ = "LONG $"+ConvertBitToHex$(o$) End Select print#2,arg$ hadCrLf = %FALSE Select Case pc& Case &h084, &h085,&h0137,&h138 print#2,"" hadCrLf = %TRUE End Select If left$(opx$,3) = "RET" Then print#2,"" hadCrLf = %TRUE else Select Case left$(opx$,4) Case "JMP " print#2,"" hadCrLf = %TRUE dst$ = "000000000" If src$ = "00000-000" Then src$ = "000001000" End If Case "LOCK" dst$ = "000000000" mid$(src$,1) = "00000" End Select End If print#3," "+FNhex$(pc&,3)+" "+l$+" "+opc$+" "+zcr$+" "+cnd$+" "+dst$+" "+src$+" "+arg$ End If Return REM ************************************************************************** REM * * REM * REM * * REM ************************************************************************** DisplayMapping: ip$ = "" op$ = "" total& = 0 For i& = 1 To 32 If i& > 1 Then ip$ = ip$+" " End If ip$ = ip$+FNnum$(32-i&,2) Print#2, FNstr$(32-i&),Mid$("opcodezcriccccdddddddddsssssssss",i&,1),mapFrom$(i&); mapBit& = 0 For j& = 1 To 32 If mid$(mapFrom$(i&),j&,1) <> "-" Then If mapBit& = 0 Then mapBit& = j& else mapBit& = -1 End If End If Next Select Case mapBit& Case 0 : print#2,"","ERROR !!!" Print "Conflict ERROR in bit-mapping" Case -1 : Print#2,"" Case Else : print#2,"","Solved";32-mapBit& total& = total&+1 If i& > 1 Then op$ = op$+" " End If op$ = op$+FNnum$(32-mapBit&,2) End Select Next print#2,"" print#2,"Solved";total&;"bit-mappings leaving";32-total&;"to resolve" print#2,"" print#2,"Instruction Bit : "+left$(ip$,47) print#2,"Encoded ROM Bit : "+left$(op$,47) print#2,"" print#2,"Instruction Bit : "+right$(ip$,47) print#2,"Encoded ROM Bit : "+right$(op$,47) print#2,"" print "Solved";total&;"bit-mappings leaving";32-total&;"to resolve" Return Sub SetMapping(encrypted$,target$) Shared mapFrom$() Local i& Local j& For i& = 1 To 32 Select Case Mid$(target$,i&,1) Case "0" For j& = 1 To 32 If Mid$(encrypted$,j&,1) <> "0" Then Mid$(mapFrom$(i&),j&) = "-" End If Next Case "1" For j& = 1 To 32 If Mid$(encrypted$,j&,1) <> "1" Then Mid$(mapFrom$(i&),j&) = "-" End If Next End Select Next End Sub Function DecodeInstruction$(n$) Shared mapFrom$() Local i& Local j& Local thisBit$ Local o$ o$ = "" For i& = 1 To 32 thisBit$ = "?" For j& = 1 To 32 If Mid$(mapFrom$(i&),j&,1) <> "-" Then If thisBit$ = "?" Then thisbit$ = Mid$(n$,j&,1) else If thisbit$ <> Mid$(n$,j&,1) Then thisBit$ = "-" End If End If End If Next o$ = o$ + thisbit$ Next DecodeInstruction$ = o$ End Function REM ************************************************************************** REM * * REM * REM * * REM ************************************************************************** AddCopyRightNotice: print#2,"" print#2,"Binary Image Information is Copyright 2005 Parallax Inc. All Rights Reserved" print#2,"============================================================================" print#2,"" print#2,"The information within this document may not be used for any commercial" print#2,"purpose without the written permission of Parallax Inc." print#2,"" print#2,"This document may not be publicly distributed beyond official Parallax Inc." print#2,"Forums without the written permission of Parallax Inc." print#2,"" print#2,"No liability whatsoever is accepted for any errors or omissions in this" print#2,"document by Parallax Inc. or contributing authors." print#2,"" Return Function Trim$(n$) Trim$ = LTrim$(RTrim$(n$)) End Function Function RemoveSpace$(n$) Local i& Local o$ o$ = Trim$(n$) i& = Instr(o$," ") While i& > 0 o$ = Left$(o$,i&-1)+Right$(o$,Len(o$)-i&) i& = Instr(o$," ") Wend RemoveSpace$ = o$ End Function Function ConvertHexToBit$(n$) Local i& Local o$ o$ = "" For i& = 1 To Len(n$) o$ = o$+FNbin$(Val("&h"+Mid$(n$,i&,1)),4) Next ConvertHexToBit$ = o$ End Function Def FNstr$(n&) FNstr$ = LTRim$(Str$(n&)) End Def DEF FNnum$(n&,w&) FNnum$ = Right$(space$(w&)+Str$(n&),w&) End DEf Def FNbin$(n&,w&) FNbin$ = Right$(String$(w&,"0")+Bin$(n&),w&) End Def Def FNhex$(n&,w&) FNhex$ = Right$(String$(w&,"0")+Hex$(n&),w&) End Def Function ConvertBitToHex$(tt$) local o$ local i& local t$ t$ = tt$ while (len(t$) mod 4 ) <> 0 t$ = "0"+t$ wend o$ = "" for i& = 1 to len(t$)-1 step 4 If i& = 17 Then o$ = o$+"_" End If Select Case mid$(t$,i&,4) Case "0000" : o$ = o$+"0" Case "0001" : o$ = o$+"1" Case "0010" : o$ = o$+"2" Case "0011" : o$ = o$+"3" Case "0100" : o$ = o$+"4" Case "0101" : o$ = o$+"5" Case "0110" : o$ = o$+"6" Case "0111" : o$ = o$+"7" Case "1000" : o$ = o$+"8" Case "1001" : o$ = o$+"9" Case "1010" : o$ = o$+"A" Case "1011" : o$ = o$+"B" Case "1100" : o$ = o$+"C" Case "1101" : o$ = o$+"D" Case "1110" : o$ = o$+"E" Case "1111" : o$ = o$+"F" Case Else : o$ = o$+"-" End Select next ConvertBitToHex$ = o$ end Function REM ************************************************************************** REM * * REM * REM * * REM ************************************************************************** DEF FNifs$(n$) local o$ Select Case n$ Case "0000" : o$ = "IF_NEVER " ' 0000 - Never Case "0001" : o$ = "IF_NC_AND_NZ " ' 0001 Case "0010" : o$ = "IF_NC_AND_Z " ' 0010 Case "0011" : o$ = "IF_NC " ' 0011 Case "0100" : o$ = "IF_C_AND_NZ " ' 0100 Case "0101" : o$ = "IF_NZ " ' 0101 Case "0110" : o$ = "IF_C_NE_Z " ' 0110 Case "0111" : o$ = "IF_NC_OR_NZ " ' 0111 Case "1000" : o$ = "IF_C_AND_Z " ' 1000 Case "1001" : o$ = "IF_C_EQ_Z " ' 1001 Case "1010" : o$ = "IF_Z " ' 1010 Case "1011" : o$ = "IF_NC_OR_Z " ' 1011 Case "1100" : o$ = "IF_C " ' 1100 Case "1101" : o$ = "IF_C_OR_NZ " ' 1101 Case "1110" : o$ = "IF_C_OR_Z " ' 1110 Case "1111" : o$ = " " ' 1111 - ALWAYS Case Else : o$ = "IF_? " End Select FNifs$=o$ end def DEF FNopc$(n$) local o$ local i& local x$ Select Case n$ Case "000000" : o$="?w?iWRBYTE d,s?iWRBYTE d,#m?w?iRDBYTE d,s?iRDBYTE d,#m" Case "000001" : o$="?w?iWRWORD d,s?iWRWORD d,#m?w?iRDWORD d,s?iRDWORD d,#m" Case "000010" : o$="?w?iWRLONG d,s?iWRLONG d,#m?w?iRDLONG d,s?iRDLONG d,#m" Case "000011" : o$="?iHUBOP d,s?i?0CLKSET d?0?1COGID d?1?2COGINIT d?2?3COGSTOP d?3?4LOCKNEW d?4?5LOCKRET d?5?6LOCKSET d?6?7LOCKCLR d?7HUBOP d,s" Case "000100" : o$="" Case "000101" : o$="" Case "000110" : o$="" Case "000111" : o$="" Case "001000" : o$="ROR d,s +r" Case "001001" : o$="ROL d,s +r" Case "001010" : o$="SHR d,s +r" Case "001011" : o$="SHL d,s +r" Case "001100" : o$="RCR d,s +r" Case "001101" : o$="RCL d,s +r" Case "001110" : o$="SAR d,s +r" Case "001111" : o$="REV d,s +r" Case "010000" : o$="MINS d,s +r" Case "010001" : o$="MAXS d,s +r" Case "010010" : o$="MIN d,s +r" Case "010011" : o$="MAX d,s +r" Case "010100" : o$="MOVS d,s +r" Case "010101" : o$="MOVD d,s +r" Case "010110" : o$="MOVI d,s +r" Case "010111" : o$="?w?iJMP s?i?sRET?sJMP #r?w?iJMPRET c,s?i?pCALL e#j?pJMPRET c,#j" Case "011000" : o$="?wTEST d,s?wAND d,s" Case "011001" : o$="ANDN d,s +r" Case "011010" : o$="OR d,s +r" Case "011011" : o$="XOR d,s +r" Case "011100" : o$="MUXC d,s +r" Case "011101" : o$="MUXNC d,s +r" Case "011110" : o$="MUXZ d,s +r" Case "011111" : o$="MUXNZ d,s +r" Case "100000" : o$="ADD d,s +r" Case "100001" : o$="?wCMP d,s?wSUB d,s" Case "100010" : o$="ADDABS d,s +r" Case "100011" : o$="SUBABS d,s +r" Case "100100" : o$="SUMC d,s +r" Case "100101" : o$="SUMNC d,s +r" Case "100110" : o$="SUMZ d,s +r" Case "100111" : o$="SUMNZ d,s +r" Case "101000" : o$="MOV d,s +r" Case "101001" : o$="NEG d,s +r" Case "101010" : o$="ABS d,s +r" Case "101011" : o$="ABSNEG d,s +r" Case "101100" : o$="NEGC d,s +r" Case "101101" : o$="NEGNC d,s +r" Case "101110" : o$="NEGZ d,s +r" Case "101111" : o$="NEGNZ d,s +r" Case "110000" : o$="CMPS d,s -r" Case "110001" : o$="CMPSX d,s -r" Case "110010" : o$="ADDX d,s +r" Case "110011" : o$="?wCMPX d,s?wSUBX d,s" Case "110100" : o$="ADDS d,s +r" Case "110101" : o$="SUBS d,s +r" Case "110110" : o$="ADDSX d,s +r" Case "110111" : o$="SUBSX d,s +r" Case "111000" : o$="CMPSUB d,s +r" Case "111001" : o$="?iDJNZ d,s +r?iDJNZ d,#j +r" Case "111010" : o$="?iTJNZ d,s -r?iTJNZ d,#j -r" Case "111011" : o$="?iTJZ d,s -r?iTJZ d,#j -r" Case "111100" : o$="WAITPEQ d,s -r" Case "111101" : o$="WAITPNE d,s -r" Case "111110" : o$="WAITCNT d,s +r" Case "111111" : o$="WAITVID d,s -r" Case Else : o$=" d,s +r" End Select while left$(o$,1)="?" Select Case left$(o$,2) Case "?i" : x$=mid$(zcr$,4,1) Case "?w" : x$=mid$(zcr$,3,1) Case "?p" : x$="1" Case "?s" : If src$="000000000" Then x$="0" else x$="1" End If Case "?0" : If right$(src$,3)="000" Then x$="0" Else x$="1" Case "?1" : If right$(src$,3)="001" Then x$="0" Else x$="1" Case "?2" : If right$(src$,3)="010" Then x$="0" Else x$="1" Case "?3" : If right$(src$,3)="011" Then x$="0" Else x$="1" Case "?4" : If right$(src$,3)="100" Then x$="0" Else x$="1" Case "?5" : If right$(src$,3)="101" Then x$="0" Else x$="1" Case "?6" : If right$(src$,3)="110" Then x$="0" Else x$="1" Case "?7" : If right$(src$,3)="111" Then x$="0" Else x$="1" Case else print "OOPS ",o$ x$="-" End Select Select Case x$ Case "0" i&=instr(2,o$,left$(o$,2)) o$=left$(o$,i&-1) o$=right$(o$,len(o$)-2) Case "1" i&=instr(2,o$,left$(o$,2)) o$=right$(o$,len(o$)-i&) o$=right$(o$,len(o$)-1) Case else o$=" "+o$ End Select wend i&=instr(o$," ") If i&>0 Then o$=left$(left$(o$,i&-1)+space$(8),8)+right$(o$,len(o$)-i&) End If If right$(o$,2)="+r" Then o$=left$(o$,len(o$)-3)+"+c+z-r" else If right$(o$,2)="-r" Then o$=left$(o$,len(o$)-3)+"+c+z+r" else o$=o$+"+c+z" End If End If fnopc$=o$ end def DEF Fnreg$(issrc,n$) local o$ local v& local i& shared zcr$ If instr(n$,"-")<>0 Then o$="$"+ConvertBitToHex$(n$) else v&=0 for i&=1 to len(n$) v&=v&*2 If mid$(n$,i&,1)="1" Then v&=v&+1 End If next Select Case v& Case &h019 : o$="JmpVector" Case &h158 : o$="PushLong" Case &h1EB : o$="pcBase" Case &h1EC : o$="vpBase" Case &h1ED : o$="spBase" Case &h1EE : o$="pc" Case &h1EF : o$="sp" Case &h1F0 : o$="PAR" Case &h1F1 : o$="CNT" Case &h1F2 : o$="INA" Case &h1F3 : o$="INB" Case &h1F4 : o$="OUTA" Case &h1F5 : o$="OUTB" Case &h1F6 : o$="DIRA" Case &h1F7 : o$="DIRB" Case &h1F8 : o$="CTRA" Case &h1F9 : o$="CTRB" Case &h1FA : o$="FRQA" Case &h1FB : o$="FRQB" Case &h1FC : o$="PHSA" Case &h1FD : o$="PHSB" Case &h1FE : o$="VCFG" Case &h1FF : o$="VSCL" Case else If issrc and right$(zcr$,1) = "1" Then If v& <= 16 Then o$ = FNstr$(v&) else o$ = "$"+FNhex$(v&,3) End If else o$ = "$"+FNhex$(v&,3) End If End Select End If If issrc Then Select Case pc& Case &h013,&h1C1,&h1C8,&h15B : o$ = "0-0" End Select else Select Case pc& Case &h0C3 : o$ = "0-0" End Select End If FNreg$=o$ end def