diff --git a/Makefile b/Makefile index d7ec1f2..4f959eb 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -TARGETS := hello test undump +TARGETS := test undump LIBS := binary.pas ihex.pas FPC_FLAGS := -g FPC := fpc $(FPC_FLAGS) @@ -10,9 +10,6 @@ all: $(TARGETS) clean: rm -f *.o $(TARGETS) -hello: hello.pas - $(FPC) $@.pas - test: test.pas $(LIBS) $(FPC) $@.pas diff --git a/binary.pas b/binary.pas index 72d3707..2cdd42d 100644 --- a/binary.pas +++ b/binary.pas @@ -4,8 +4,10 @@ WStr = String[4]; { String representing a u16. } function ValidHexDigit(bval: Byte): Boolean; var - IsValid : Boolean = False; + IsValid : Boolean; begin + IsValid := False; + if ((bval >= $30) and (bval <= $39)) then IsValid := True; if ((bval >= $41) and (bval <= $46)) then @@ -19,7 +21,7 @@ end; function ReadDigit(bchr: Char): Byte; var - bval : Byte = 0; + bval : Byte; label exitearly; begin @@ -37,7 +39,7 @@ begin {Perform the conversion.} if (bchr >= 'A') then - bval := (bval - $41) + 10 + bval := bval - $37 else bval := bval - $30; @@ -48,7 +50,7 @@ end; function ReadByte(bstr : BStr): Byte; var - out : Byte = 0; + out : Byte; begin out := ReadDigit(bstr[1]) shl 4; out := out + ReadDigit(bstr[2]); @@ -58,9 +60,10 @@ end; function ReadWord(bstr : WStr): Integer; var - out : Integer = 0; - I : Integer = 1; + out : Integer; + I : Integer; begin + out := 0; for I := 1 to 4 do begin out := out shl 4; @@ -77,8 +80,8 @@ begin c := Char(0); if (digit < $0A) then c := Char(digit + $30) - else if (digit < $0F) then - c := Char(digit + $41); + else if (digit <= $0F) then + c := Char(digit + $37); ToDigit := c; end; @@ -86,8 +89,9 @@ end; function WriteByte(bval : Byte): BStr; var - s : BStr = '00'; + s : BStr; begin + s := '00'; s[1] := ToDigit(bval shr 4); s[2] := ToDigit(bval and $0F); WriteByte := s; @@ -95,8 +99,9 @@ end; function WriteWord(bval : Integer): WStr; var - s : WStr = '0000'; + s : WStr; begin + s := '0000'; s[1] := ToDigit((bval shr $0C) and $000F); s[2] := ToDigit((bval shr $08) and $000F); s[3] := ToDigit((bval shr $04) and $000F); diff --git a/ihex.pas b/ihex.pas index 6e4215f..17cdbcd 100644 --- a/ihex.pas +++ b/ihex.pas @@ -1,8 +1,6 @@ const -MaxLnLen : Integer = 64; -MaxRLen : Integer = 32; -DumpBytes: Integer = 16; IRecEOF : String[11] = ':00000001FF'; +IRecDat : Byte = 0; type LnStr = String[64]; @@ -13,23 +11,43 @@ irec = record Data : array [1..32] of Byte; Count : Byte; MaxCount : Byte; + RType : Byte; end; -function GetDumpCnt(Line : LnStr): Byte; +function IsWhSp(c: Char): Boolean; +var + res : Boolean; begin - -end; + res := False; + case c of + ' ' : res := True; + #$0A : res := True; + #$0D : res := True; + end; + + IsWhSp := res; +end; function RdDumpLn(Line : LnStr): irec; var lrec : irec; begin lrec.Addr := ReadWord(Line); - while (lrec.Count < DumpBytes) do + lrec.RType := IRecDat; + lrec.Count := 0; + + Delete(Line, 1, 4); + while (Line <> '') do begin - lrec.Count := lrec.Count + 1; + if IsWhSp(Line[1]) then Delete(Line, 1, 1); + if Line <> '' then + begin + lrec.Count := lrec.Count + 1; + lrec.Data[lrec.Count] := ReadByte(Line); + Delete(Line, 1, 2); + end; end; RdDumpLn := lrec; @@ -37,23 +55,41 @@ end; function IRecCS(rec : irec) : Byte; var - cksm : Integer = 0; + cksm : Integer; + I : Integer; begin - cksm := rec.Addr + rec.Count; + cksm := rec.Addr + rec.Count + rec.RType; + for I := 1 to rec.Count do cksm := cksm + rec.Data[I]; + cksm := cksm and $00FF; - IRecCS := Byte(cksm and $00FF); + IRecCS := not Byte(cksm) + 1; end; -procedure wrirec(rec : irec); +procedure PrnRec(rec : irec); var I : Byte; begin Write(':'); Write(WriteByte(rec.Count)); Write(WriteWord(rec.Addr)); - + Write(WriteByte(rec.RType)); for I := 1 to rec.Count do Write(WriteByte(rec.Data[I])); Writeln(WriteByte(IRecCS(rec))); end; + + +procedure WrRec(rec : irec; var outf : Text); +var + I : Byte; +begin + Write(outf, ':'); + Write(outf, WriteByte(rec.Count)); + Write(outf, WriteWord(rec.Addr)); + Write(outf, WriteByte(rec.RType)); + for I := 1 to rec.Count do + Write(outf, WriteByte(rec.Data[I])); + + Writeln(outf, WriteByte(IRecCS(rec))); +end; \ No newline at end of file diff --git a/test.pas b/test.pas index 1655715..31e3f0f 100644 --- a/test.pas +++ b/test.pas @@ -1,10 +1,16 @@ program test; {$I 'binary.pas'} - +var + I : Byte; begin Writeln('ReadByte(''41'')->', ReadByte('41')); Writeln('WriteByte($41)->', WriteByte($41)); Writeln('ReadWord(''4243'')->', ReadWord('4243')); Writeln('WriteByte($4243)->', WriteWord($4243)); + + for I := 0 to 32 do + begin + WriteLn(I, '->', WriteByte(I)); + end; end. diff --git a/undump.pas b/undump.pas index 4fd0431..eefdcf9 100644 --- a/undump.pas +++ b/undump.pas @@ -7,6 +7,88 @@ program undump; {$I 'binary.pas'} {$I 'ihex.pas'} +type +FilePath = String[12]; + +Procedure DumpIHex(iname, oname : FilePath); +var + inf : Text; + outf : Text; + line : LnStr; + rec : IRec; begin - Writeln('UNDUMP V1.0'); + if iname <> '' then + begin + Assign(inf, iname); + Reset(inf); + end else inf := Input; + + if oname <> '' then + begin + Assign(outf, oname); + Rewrite(outf); + end else outf := Output; + + while not EOF(inf) do + begin + ReadLn(inf, line); + if (Line <> '') then + begin + rec := RdDumpLn(line); + WrRec(rec, outf); + end; + end; + + Close(inf); + Writeln(outf, IRecEOF); + Flush(outf); + Close(outf); +end; + +procedure PrnHelp; +begin + WriteLn('UNDUMP V1.0'); + WriteLn('Converts DUMP.COM output to Intel Hex format'); + WriteLn(''); + WriteLn('Usage:'); + WriteLn(' UNDUMP.COM INFILE OUTFILE'); + Halt; +end; + +var + i, argc : Integer; + rec : irec; + opts : record + ShoVer : Boolean; + InFile : FilePath; + OutFile : FilePath; + end; + param : String[255]; +begin + opts.ShoVer := False; + opts.InFile := ''; + opts.OutFile := ''; + argc := ParamCount; + + for i := 1 to ParamCount do + begin + if ParamStr(i) = '-h' or ParamStr(i) = 'H' then + begin + opts.ShoVer := True; + argc := argc - 1; + end; + end; + + if opts.ShoVer then PrnHelp(); + + i := ParamCount - argc + 1; + + if i = 1 then opts.OutFile := ParamStr(ParamCount); + if i > 1 then + begin + InFile := ParamStr(i); + OutFile := ParamStr(i+1); + end; + + DumpIHex(InFile, OutFile); end.