From 08d702bd97deccde56db58ae8c3417de875f3212 Mon Sep 17 00:00:00 2001 From: Kyle Isom Date: Tue, 3 Oct 2023 03:07:21 -0700 Subject: [PATCH] binary functions mostly done i think --- Makefile | 2 +- binary.inc | 57 ----------------------------- binary.pas | 105 +++++++++++++++++++++++++++++++++++++++++++++++++++++ ihex.pas | 12 ++++++ test.pas | 12 +++--- 5 files changed, 123 insertions(+), 65 deletions(-) delete mode 100644 binary.inc create mode 100644 binary.pas create mode 100644 ihex.pas diff --git a/Makefile b/Makefile index 933337b..1421eba 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ TARGETS := hello test -LIBS := binary.inc +LIBS := binary.pas ihex.pas FPC_FLAGS := -g FPC := fpc $(FPC_FLAGS) diff --git a/binary.inc b/binary.inc deleted file mode 100644 index 045f1fc..0000000 --- a/binary.inc +++ /dev/null @@ -1,57 +0,0 @@ -type - ByteString = String[2]; { String representing a u8. } - ShortString = String[4]; { String representing a u16. } - - -function ValidHexDigit(bval: Byte): Boolean; -var - IsValid : Boolean = False; -begin - if ((bval >= $30) and (bval <= $39)) then - IsValid := True; - if ((bval >= $41) and (bval <= $46)) then - IsValid := True; - if ((bval >= $61) and (bval <= $66)) then - IsValid := True; - - ValidHexDigit := IsValid; -end; - - -function ReadDigit(bchr: Char): Byte; -var - bval : Byte = 0; -label - exitearly; -begin - bval := Byte(bchr); - - if (not ValidHexDigit(bval)) then - begin - bval := 0; - goto exitearly; - end; - - {Normalize downcase digits to upcase digits.} - if (bchr >= 'a') then - bval := bval xor $20; - - {Perform the conversion.} - if (bchr >= 'A') then - bval := (bval - $41) + 10 - else - bval := bval - $30; - -exitearly: - ReadDigit := bval; -end; - - -function ReadByte(bstr : ByteString): Byte; -var - out : Byte = 0; -begin - out := ReadDigit(bstr[1]) shl 4; - out := out + ReadDigit(bstr[2]); - ReadByte := out; -end; diff --git a/binary.pas b/binary.pas new file mode 100644 index 0000000..72d3707 --- /dev/null +++ b/binary.pas @@ -0,0 +1,105 @@ +type +BStr = String[2]; { String representing a u8. } +WStr = String[4]; { String representing a u16. } + +function ValidHexDigit(bval: Byte): Boolean; +var + IsValid : Boolean = False; +begin + if ((bval >= $30) and (bval <= $39)) then + IsValid := True; + if ((bval >= $41) and (bval <= $46)) then + IsValid := True; + if ((bval >= $61) and (bval <= $66)) then + IsValid := True; + + ValidHexDigit := IsValid; +end; + + +function ReadDigit(bchr: Char): Byte; +var + bval : Byte = 0; +label + exitearly; +begin + bval := Byte(bchr); + + if (not ValidHexDigit(bval)) then + begin + bval := 0; + goto exitearly; + end; + + {Normalize downcase digits to upcase digits.} + if (bchr >= 'a') then + bval := bval xor $20; + + {Perform the conversion.} + if (bchr >= 'A') then + bval := (bval - $41) + 10 + else + bval := bval - $30; + +exitearly: + ReadDigit := bval; +end; + + +function ReadByte(bstr : BStr): Byte; +var + out : Byte = 0; +begin + out := ReadDigit(bstr[1]) shl 4; + out := out + ReadDigit(bstr[2]); + ReadByte := out; +end; + + +function ReadWord(bstr : WStr): Integer; +var + out : Integer = 0; + I : Integer = 1; +begin + for I := 1 to 4 do + begin + out := out shl 4; + out := out + Integer(ReadDigit(bstr[I])); + end; + ReadWord := out; +end; + + +function ToDigit(digit : Byte): Char; +var + c : Char; +begin + c := Char(0); + if (digit < $0A) then + c := Char(digit + $30) + else if (digit < $0F) then + c := Char(digit + $41); + + ToDigit := c; +end; + + +function WriteByte(bval : Byte): BStr; +var + s : BStr = '00'; +begin + s[1] := ToDigit(bval shr 4); + s[2] := ToDigit(bval and $0F); + WriteByte := s; +end; + +function WriteWord(bval : Integer): WStr; +var + s : WStr = '0000'; +begin + s[1] := ToDigit((bval shr $0C) and $000F); + s[2] := ToDigit((bval shr $08) and $000F); + s[3] := ToDigit((bval shr $04) and $000F); + s[4] := ToDigit(bval and $000F); + WriteWord := s; +end; diff --git a/ihex.pas b/ihex.pas new file mode 100644 index 0000000..aed3542 --- /dev/null +++ b/ihex.pas @@ -0,0 +1,12 @@ +const +MaxRLen = 32; + +type +{irec stores an individual record.} +irec = record + Addr : Integer; + Data : array[1...MaxRLen] of Byte; + MaxCount : Byte; + +{procedure RdDumpLn;} +{procedure wrirec;} diff --git a/test.pas b/test.pas index 6b90725..1655715 100644 --- a/test.pas +++ b/test.pas @@ -1,12 +1,10 @@ program test; -{$I 'binary.inc'} - -var - bstr : String[2] = '41'; - bval : Byte; +{$I 'binary.pas'} begin - bval := ReadByte(bstr); - Writeln('hello', Char(bval)); + Writeln('ReadByte(''41'')->', ReadByte('41')); + Writeln('WriteByte($41)->', WriteByte($41)); + Writeln('ReadWord(''4243'')->', ReadWord('4243')); + Writeln('WriteByte($4243)->', WriteWord($4243)); end.