MegaProcessor

Check-in [30305c1cdb]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Moved ihex-generating functions into their own separate module. Added some cleverness to allow tables as well as strings on ihex_datarecord()
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:30305c1cdb8d91e4cf2912b5bc5e35cf501e3884
User & Date: jos 2015-08-07 14:50:18
Context
2015-08-09
13:41
moved a one-off test-tool out of the way. check-in: 4ddbdf4b00 user: jos tags: trunk
2015-08-07
14:50
Moved ihex-generating functions into their own separate module. Added some cleverness to allow tables as well as strings on ihex_datarecord() check-in: 30305c1cdb user: jos tags: trunk
2015-08-03
21:58
Moved executables to new subdirectory 'bin'. check-in: 4c181d3d35 user: jos tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Added ihex.lua.

































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
--
-- Usage: require('thisfile');
--

local ihex_address = 0x0000;

function ihex_reset()
	ihex_address = 0x0000;
end

function ihex_set( value )
    ihex_address = value;
end

function ihex_chksum( buf,startVal )
	local ourSum = startVal or 0;

	for i=1,#buf,1 do
		ourSum = ourSum + buf:byte(i);
	end--for

	ourSum = ourSum + bit32.band(ihex_address,0xff);
	ourSum = ourSum + bit32.band( bit32.rshift(ihex_address,8),0xff);

	ourSum = bit32.bnot(ourSum) + 1;
	ourSum = bit32.band(ourSum,0xff);

	return( ourSum );
end

function table.byte(t,i)
    return( t[i] );
end

function table.len(t)
    return( #t );
end
--
-- Input : string or an array of byte-values and an optional length
-- If the first parameter is a table, then it gets 2 additional method-fields
-- that type 'string' already has by itself.
--
-- Outputs a ihex-format record and updates global ihex_address
--
function ihex_datarecord(buf,len)
    if( type(buf) == 'table' ) then
        buf.byte = table.byte; buf.len = table.len;
    end

	local ourLen = len or buf:len();
	local ourSum = ihex_chksum(buf,ourLen);
	io.write( string.format(':%02X%04X00',ourLen,ihex_address) );
	for i=1,ourLen do io.write( string.format('%02X',buf:byte(i)) ); end

	ihex_address = ihex_address + ourLen;
	io.write( string.format('%02X', ourSum ) );
	io.write( '\n' );
end

function ihex_endrecord()
	io.write(':00000001FF\n');
end

-- EOF --

Changes to tools/clr_intram.lua.

1
2

3


4
5
6
7
8
9
10
11
12
13
14
15
16
17

18
19
20

21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
..
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env lua
local to_byte = function (x) return string.char( bit32.band(x,0xff) ); end  --returns a 1-byte string

local as_byte = string.byte;



-- [first] begin dump at 16 byte-aligned offset containing 'first' byte
-- [last] end dump at 16 byte-aligned offset containing 'last' byte
function hex_dump(buf,first,last)
	local function align(n) return math.ceil(n/16) * 16 end
	for i=(align((first or 1)-16)+1),align(math.min(last or #buf,#buf)) do
		if (i-1) % 16 == 0 then io.write(string.format('%08X  ', i-1)) end
		io.write( i > #buf and '   ' or string.format('%02X ', buf:byte(i)) )
		if i %  8 == 0 then io.write(' ') end
		if i % 16 == 0 then io.write( buf:sub(i-16+1, i):gsub('%c','.'), '\n' ) end
	end
end



local ihex_address = 0x0000;

function ihex_reset()

	ihex_address = 0x0000;
end

function ihex_chksum( buf,startVal )
	local ourSum = startVal or 0;

	for i=1,#buf,1 do
		ourSum = ourSum + buf:byte(i);
	end--for

	ourSum = ourSum + bit32.band(ihex_address,0xff);
	ourSum = ourSum + bit32.band( bit32.rshift(ihex_address,8),0xff);

	ourSum = bit32.bnot(ourSum) + 1;
	ourSum = bit32.band(ourSum,0xff);

	return( ourSum );
end

function ihex_datarecord(buf,len)
	local ourLen = len or 16;
	local ourSum = ihex_chksum(buf,ourLen);
	io.write( string.format(':%02X%04X00',ourLen,ihex_address) );
	for i=1,ourLen do io.write( string.format('%02X',buf:byte(i)) ); end

	ihex_address = ihex_address + ourLen;
	io.write( string.format('%02X', ourSum ) );
	io.write( '\n' );
end

function ihex_endrecord()
	io.write(':00000001FF\n');
end

function ByteBuf( array )
	local ourBuf = {};
	for i,v in ipairs(array) do ourBuf[i] = to_byte(v); end
	return( table.concat(ourBuf) );
end

-- Main line:
................................................................................
        0x98,               -- ST.W (R2++), R0
        0x59,               -- ADDQ R1, #-2
        0xe6, 0xfc,         -- BNE *-4
        0xf3, 0x0b, 0x00    -- JMP 0x000b
    };

ihex_reset();

Prg = ByteBuf(Prg);
ihex_datarecord(Prg,#Prg);
ihex_endrecord();


<
>
|
>
>













<
>
|

<
>
|
<
<
<
<
<
<
<
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







 







<
|
|


1

2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

19
20
21

22
23







24

























25
26
27
28
29
30
31
..
36
37
38
39
40
41
42

43
44
45
46
#!/usr/bin/env lua


package.path = '../?.lua;' .. package.path;
require('ihex');


-- [first] begin dump at 16 byte-aligned offset containing 'first' byte
-- [last] end dump at 16 byte-aligned offset containing 'last' byte
function hex_dump(buf,first,last)
	local function align(n) return math.ceil(n/16) * 16 end
	for i=(align((first or 1)-16)+1),align(math.min(last or #buf,#buf)) do
		if (i-1) % 16 == 0 then io.write(string.format('%08X  ', i-1)) end
		io.write( i > #buf and '   ' or string.format('%02X ', buf:byte(i)) )
		if i %  8 == 0 then io.write(' ') end
		if i % 16 == 0 then io.write( buf:sub(i-16+1, i):gsub('%c','.'), '\n' ) end
	end
end


local to_byte = function (x) return string.char( bit32.band(x,0xff) ); end  --returns a 1-byte string
local as_byte = string.byte;


--
-- Convert an array of byte-values into a string of corresponding bytes.







--

























function ByteBuf( array )
	local ourBuf = {};
	for i,v in ipairs(array) do ourBuf[i] = to_byte(v); end
	return( table.concat(ourBuf) );
end

-- Main line:
................................................................................
        0x98,               -- ST.W (R2++), R0
        0x59,               -- ADDQ R1, #-2
        0xe6, 0xfc,         -- BNE *-4
        0xf3, 0x0b, 0x00    -- JMP 0x000b
    };

ihex_reset();

-- Prg = ByteBuf(Prg);
ihex_datarecord(Prg);
ihex_endrecord();