working undump

This commit is contained in:
Kyle Isom 2023-10-03 16:19:22 -07:00
parent 0e42524d76
commit a43054deec
5 changed files with 155 additions and 29 deletions

View File

@ -1,4 +1,4 @@
TARGETS := hello test undump TARGETS := test undump
LIBS := binary.pas ihex.pas LIBS := binary.pas ihex.pas
FPC_FLAGS := -g FPC_FLAGS := -g
FPC := fpc $(FPC_FLAGS) FPC := fpc $(FPC_FLAGS)
@ -10,9 +10,6 @@ all: $(TARGETS)
clean: clean:
rm -f *.o $(TARGETS) rm -f *.o $(TARGETS)
hello: hello.pas
$(FPC) $@.pas
test: test.pas $(LIBS) test: test.pas $(LIBS)
$(FPC) $@.pas $(FPC) $@.pas

View File

@ -4,8 +4,10 @@ WStr = String[4]; { String representing a u16. }
function ValidHexDigit(bval: Byte): Boolean; function ValidHexDigit(bval: Byte): Boolean;
var var
IsValid : Boolean = False; IsValid : Boolean;
begin begin
IsValid := False;
if ((bval >= $30) and (bval <= $39)) then if ((bval >= $30) and (bval <= $39)) then
IsValid := True; IsValid := True;
if ((bval >= $41) and (bval <= $46)) then if ((bval >= $41) and (bval <= $46)) then
@ -19,7 +21,7 @@ end;
function ReadDigit(bchr: Char): Byte; function ReadDigit(bchr: Char): Byte;
var var
bval : Byte = 0; bval : Byte;
label label
exitearly; exitearly;
begin begin
@ -37,7 +39,7 @@ begin
{Perform the conversion.} {Perform the conversion.}
if (bchr >= 'A') then if (bchr >= 'A') then
bval := (bval - $41) + 10 bval := bval - $37
else else
bval := bval - $30; bval := bval - $30;
@ -48,7 +50,7 @@ end;
function ReadByte(bstr : BStr): Byte; function ReadByte(bstr : BStr): Byte;
var var
out : Byte = 0; out : Byte;
begin begin
out := ReadDigit(bstr[1]) shl 4; out := ReadDigit(bstr[1]) shl 4;
out := out + ReadDigit(bstr[2]); out := out + ReadDigit(bstr[2]);
@ -58,9 +60,10 @@ end;
function ReadWord(bstr : WStr): Integer; function ReadWord(bstr : WStr): Integer;
var var
out : Integer = 0; out : Integer;
I : Integer = 1; I : Integer;
begin begin
out := 0;
for I := 1 to 4 do for I := 1 to 4 do
begin begin
out := out shl 4; out := out shl 4;
@ -77,8 +80,8 @@ begin
c := Char(0); c := Char(0);
if (digit < $0A) then if (digit < $0A) then
c := Char(digit + $30) c := Char(digit + $30)
else if (digit < $0F) then else if (digit <= $0F) then
c := Char(digit + $41); c := Char(digit + $37);
ToDigit := c; ToDigit := c;
end; end;
@ -86,8 +89,9 @@ end;
function WriteByte(bval : Byte): BStr; function WriteByte(bval : Byte): BStr;
var var
s : BStr = '00'; s : BStr;
begin begin
s := '00';
s[1] := ToDigit(bval shr 4); s[1] := ToDigit(bval shr 4);
s[2] := ToDigit(bval and $0F); s[2] := ToDigit(bval and $0F);
WriteByte := s; WriteByte := s;
@ -95,8 +99,9 @@ end;
function WriteWord(bval : Integer): WStr; function WriteWord(bval : Integer): WStr;
var var
s : WStr = '0000'; s : WStr;
begin begin
s := '0000';
s[1] := ToDigit((bval shr $0C) and $000F); s[1] := ToDigit((bval shr $0C) and $000F);
s[2] := ToDigit((bval shr $08) and $000F); s[2] := ToDigit((bval shr $08) and $000F);
s[3] := ToDigit((bval shr $04) and $000F); s[3] := ToDigit((bval shr $04) and $000F);

View File

@ -1,8 +1,6 @@
const const
MaxLnLen : Integer = 64;
MaxRLen : Integer = 32;
DumpBytes: Integer = 16;
IRecEOF : String[11] = ':00000001FF'; IRecEOF : String[11] = ':00000001FF';
IRecDat : Byte = 0;
type type
LnStr = String[64]; LnStr = String[64];
@ -13,23 +11,43 @@ irec = record
Data : array [1..32] of Byte; Data : array [1..32] of Byte;
Count : Byte; Count : Byte;
MaxCount : Byte; MaxCount : Byte;
RType : Byte;
end; end;
function GetDumpCnt(Line : LnStr): Byte; function IsWhSp(c: Char): Boolean;
var
res : Boolean;
begin begin
res := False;
end;
case c of
' ' : res := True;
#$0A : res := True;
#$0D : res := True;
end;
IsWhSp := res;
end;
function RdDumpLn(Line : LnStr): irec; function RdDumpLn(Line : LnStr): irec;
var var
lrec : irec; lrec : irec;
begin begin
lrec.Addr := ReadWord(Line); lrec.Addr := ReadWord(Line);
while (lrec.Count < DumpBytes) do lrec.RType := IRecDat;
lrec.Count := 0;
Delete(Line, 1, 4);
while (Line <> '') do
begin 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; end;
RdDumpLn := lrec; RdDumpLn := lrec;
@ -37,23 +55,41 @@ end;
function IRecCS(rec : irec) : Byte; function IRecCS(rec : irec) : Byte;
var var
cksm : Integer = 0; cksm : Integer;
I : Integer;
begin 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; end;
procedure wrirec(rec : irec); procedure PrnRec(rec : irec);
var var
I : Byte; I : Byte;
begin begin
Write(':'); Write(':');
Write(WriteByte(rec.Count)); Write(WriteByte(rec.Count));
Write(WriteWord(rec.Addr)); Write(WriteWord(rec.Addr));
Write(WriteByte(rec.RType));
for I := 1 to rec.Count do for I := 1 to rec.Count do
Write(WriteByte(rec.Data[I])); Write(WriteByte(rec.Data[I]));
Writeln(WriteByte(IRecCS(rec))); Writeln(WriteByte(IRecCS(rec)));
end; 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;

View File

@ -1,10 +1,16 @@
program test; program test;
{$I 'binary.pas'} {$I 'binary.pas'}
var
I : Byte;
begin begin
Writeln('ReadByte(''41'')->', ReadByte('41')); Writeln('ReadByte(''41'')->', ReadByte('41'));
Writeln('WriteByte($41)->', WriteByte($41)); Writeln('WriteByte($41)->', WriteByte($41));
Writeln('ReadWord(''4243'')->', ReadWord('4243')); Writeln('ReadWord(''4243'')->', ReadWord('4243'));
Writeln('WriteByte($4243)->', WriteWord($4243)); Writeln('WriteByte($4243)->', WriteWord($4243));
for I := 0 to 32 do
begin
WriteLn(I, '->', WriteByte(I));
end;
end. end.

View File

@ -7,6 +7,88 @@ program undump;
{$I 'binary.pas'} {$I 'binary.pas'}
{$I 'ihex.pas'} {$I 'ihex.pas'}
type
FilePath = String[12];
Procedure DumpIHex(iname, oname : FilePath);
var
inf : Text;
outf : Text;
line : LnStr;
rec : IRec;
begin 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. end.