kcpmtk/ihex.pas

95 lines
1.8 KiB
Plaintext

const
IRecEOF : String[11] = ':00000001FF';
IRecDat : Byte = 0;
type
LnStr = String[64];
{irec stores an individual record.}
irec = record
Addr : Integer;
Data : array [1..32] of Byte;
Count : Byte;
MaxCount : Byte;
RType : Byte;
end;
function IsWhSp(c: Char): Boolean;
var
res : Boolean;
begin
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);
lrec.RType := IRecDat;
lrec.Count := 0;
Delete(Line, 1, 4);
while (Line <> '') do
begin
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;
end;
function IRecCS(rec : irec) : Byte;
var
cksm : Integer;
I : Integer;
begin
cksm := rec.Addr + rec.Count + rec.RType;
for I := 1 to rec.Count do cksm := cksm + rec.Data[I];
cksm := cksm and $00FF;
IRecCS := not Byte(cksm) + 1;
end;
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;