MegaProcessor

Check-in [d3fe8703aa]
Login

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

Overview
Comment:Started to split the translation process in 2 separate passes.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:d3fe8703aaf6b5ca6d530cde730c09ffe5dd6e1b
User & Date: jos 2015-08-20 22:46:56
Context
2015-08-22
16:17
Attempted improvements on handling of db, dw, dl directives. check-in: 8769e987b7 user: jos tags: trunk
2015-08-20
22:46
Started to split the translation process in 2 separate passes. check-in: d3fe8703aa user: jos tags: trunk
21:20
Properly report the (impropable) case of an empty symbol table. check-in: 78c4228b9b user: jos tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to asm.lua.

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
..
37
38
39
40
41
42
43
44
45
46
47
48
49











50












51
52
53
54
55
56
57
require ('./lib');
require ('./symtab');
local Directive = require("./asm_instr");
require( './opcodes' );


LineNr = 1;


function ProcessFile( inFileName )
    local inFile = Check( io.open(inFileName,"r"),
                        "Can't open file " .. inFileName
                        );













    for l in inFile:lines() do
        Printf("%04d : ", LineNr ); 
        ProcessLine( Chomp(l) );
        LineNr = LineNr + 1;
    end

    symtab_dump();
end

function Fields( line )
    if( line == "" or line:match("^%s*[;//]") ) then return 0; end;
    local retVal = 0;
    local n, n1, label, instr, dst, src;
................................................................................
    end
    if( dst ) then n=n1; retVal = retVal + 1; end
    src,n1  = line:match("[%s,]+([^%s;]+)()",n);
    if( src ) then n=n1; retVal = retVal + 1; end
    return retVal, label, instr, dst, src;
end

function ProcessLine( l )
    local asm_instr;
    local n,label,Mnemonic,op1,op2 = Fields(l);
    local b1, b2, b3;
    local insLen = 0;












    if( label and not Mnemonic ) then symtab_addLabel(label) end;













    if( not Mnemonic ) then Printf("%s : %s\n", PC_get(), l ); return; end

    Mnemonic = Mnemonic:upper();

    asm_instr = Directive[ Mnemonic ];
    if( asm_instr ) then asm_instr(label,Mnemonic,op1,l); return; end







>






>
>
>
>
>
>
>
>
>
>
>
>





|







 







|





>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>







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
..
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
require ('./lib');
require ('./symtab');
local Directive = require("./asm_instr");
require( './opcodes' );


LineNr = 1;
AssemblerPass = 1;

function ProcessFile( inFileName )
    local inFile = Check( io.open(inFileName,"r"),
                        "Can't open file " .. inFileName
                        );

-- Pass 1
    for l in inFile:lines() do
        Pass1_line( Chomp(l) );
        LineNr = LineNr + 1;
    end

    inFile:seek("set",0) -- rewind input file

-- [[ Pass 2
    LineNr = 1;
    AssemblerPass = 2;
    PC_reset();
    for l in inFile:lines() do
        Printf("%04d : ", LineNr ); 
        ProcessLine( Chomp(l) );
        LineNr = LineNr + 1;
    end
--]]
    symtab_dump();
end

function Fields( line )
    if( line == "" or line:match("^%s*[;//]") ) then return 0; end;
    local retVal = 0;
    local n, n1, label, instr, dst, src;
................................................................................
    end
    if( dst ) then n=n1; retVal = retVal + 1; end
    src,n1  = line:match("[%s,]+([^%s;]+)()",n);
    if( src ) then n=n1; retVal = retVal + 1; end
    return retVal, label, instr, dst, src;
end

function Pass1_line( l ) -- Processing for pass 1, gathering of all symbols and labels.
    local asm_instr;
    local n,label,Mnemonic,op1,op2 = Fields(l);
    local b1, b2, b3;
    local insLen = 0;

    -- Printf( "%s:%s - %s - %s - %s - %s\n",PC_get(),label,Mnemonic,op1,op2,l);

    if( not Mnemonic ) then return; end;

    Mnemonic = Mnemonic:upper();

    asm_instr = Directive[ Mnemonic ];
    if( asm_instr ) then asm_instr(label,Mnemonic,op1,l); return; end
    
    asm_instr = CPU_instr[ Mnemonic ];
    if( asm_instr ) then
        if( label ) then symtab_addLabel(label) end;
        insLen = asm_instr[1];
        if( type(insLen)== 'function' ) then insLen = insLen(Mnemonic,op1,op2) end;
    end

    PC_add(insLen);
end

function ProcessLine( l ) -- Processing for pass 2
    local asm_instr;
    local n,label,Mnemonic,op1,op2 = Fields(l);
    local b1, b2, b3;
    local insLen = 0;

    if( not Mnemonic ) then Printf("%s : %s\n", PC_get(), l ); return; end

    Mnemonic = Mnemonic:upper();

    asm_instr = Directive[ Mnemonic ];
    if( asm_instr ) then asm_instr(label,Mnemonic,op1,l); return; end

Changes to asm_instr.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
require('./symtab');

local M = {
['DB'] = function( lbl,mnem,op1,srcLine ) 
    Printf("%s\n", srcLine);
    symtab_addLabel( lbl );
    PC_add(1);
    return 'DB';
end,

['DL'] = function( lbl,mnem,op1,srcLine ) 
    Printf("%s\n", srcLine);
    return 'DL';
end,
................................................................................

['DM'] = function( lbl,mnem,op1,srcLine ) 
    Printf("%s\n", srcLine);
    return 'DM';
end,

['DS'] = function( lbl,mnem,op1,srcLine ) 
    Printf("%s\n", srcLine);
    symtab_addLabel( lbl );

    return 'DS';
end,

['DW'] = function( lbl,mnem,op1,srcLine ) 
    Printf("%s\n", srcLine);
    symtab_addLabel( lbl );
    PC_add(2);
    return 'DW';
end,

['EQU'] = function( lbl,mnem,op1,srcLine ) 
    Printf("%s\n", srcLine);
    symtab_add( lbl, tonumber(op1) );




    
    return 'EQU';
end,

['ORG'] = function( lbl,mnem,op1,srcLine ) 
    Printf("%s\n", srcLine);


    return 'ORG';
end

};

-- End module
return M;





|
|







 







|
|
>





|






<
>
>
>
>






>
>







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
require('./symtab');

local M = {
['DB'] = function( lbl,mnem,op1,srcLine ) 
    Printf("%s\n", srcLine);
    if( lbl ~= nil and AssemblerPass==1 ) then symtab_addLabel( lbl ); end;
    PC_add(1); -- TODO, count and evaluate the number of fields and PC_add that result instead.
    return 'DB';
end,

['DL'] = function( lbl,mnem,op1,srcLine ) 
    Printf("%s\n", srcLine);
    return 'DL';
end,
................................................................................

['DM'] = function( lbl,mnem,op1,srcLine ) 
    Printf("%s\n", srcLine);
    return 'DM';
end,

['DS'] = function( lbl,mnem,op1,srcLine ) 
    if( op1 == nil ) then Error("DS must specify a number of bytes"); end;--if
    if( lbl ~= nil and AssemblerPass==1 ) then symtab_addLabel( lbl ); end;
    PC_add( tonumber(op1) ); -- TODO more advanced evaluation of op1.
    return 'DS';
end,

['DW'] = function( lbl,mnem,op1,srcLine ) 
    Printf("%s\n", srcLine);
    if( lbl ~= nil and AssemblerPass==1 ) then symtab_addLabel( lbl ); end;
    PC_add(2);
    return 'DW';
end,

['EQU'] = function( lbl,mnem,op1,srcLine ) 
    Printf("%s\n", srcLine);


    if( op1 == "$" ) then op1 = PC_get(); end;

    if( lbl ~= nil and AssemblerPass==1 ) then symtab_addLabel( lbl ); end;
    
    return 'EQU';
end,

['ORG'] = function( lbl,mnem,op1,srcLine ) 
    Printf("%s\n", srcLine);
    PC_set( tonumber(op1) ); -- TODO more advanced evaluation of op1.
    if( lbl ~= nil and AssemblerPass==1 ) then symtab_addLabel( lbl ); end;
    return 'ORG';
end

};

-- End module
return M;

Changes to lib.lua.

4
5
6
7
8
9
10
11
12




13
14
15
16
17
18
19
--    as early as possible, preferrably before all other requires,
--

function Printf( format, ... )
    io.write( string.format(format, ... ) );
end

function Error( text )
    Printf("Line %04d : %s\n", LineNr, text );




    os.exit(-1);
end

function htob_2( s )
    local hi,lo = s:byte(1,2);
    local hiA,loA;








|

>
>
>
>







4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
--    as early as possible, preferrably before all other requires,
--

function Printf( format, ... )
    io.write( string.format(format, ... ) );
end

function Warn( text )
    Printf("Line %04d : %s\n", LineNr, text );
end

function Error( text )
    Warn( text );
    os.exit(-1);
end

function htob_2( s )
    local hi,lo = s:byte(1,2);
    local hiA,loA;

Changes to symtab.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
--
-- Usage: require 'thisfile'
--
--


local symtab = {};

function symtab_add(symbol,value)
    if( symtab[symbol] ) then
        print( "Redefining symbol" );
        -- os.exit();
    end
    symtab[symbol] = value;

end

function symtab_addLabel( label )

    label = label or ( '_L' .. PC_get() );
    if( label:sub(-1,-1) == ':' ) then label = label:sub(1,-2) end;
    symtab_add( label, ProgramCounter );
end

function symtab_lookup(symbol)
    return( symtab[symbol] )
end

function symtab_dump()
    Printf( "\n-------- SYMBOLS -------\n" );
    if( #symtab == 0 ) then Printf("no symbols defined\n"); return; end;

    for k,v in pairs(symtab) do
        Printf("%32.32s : 0x%04x\n", k, v );
    end
end

ProgramCounter = 0;





|




|
<


>



>











|







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
--
-- Usage: require 'thisfile'
--
--

local symCount = 0;
local symtab = {};

function symtab_add(symbol,value)
    if( symtab[symbol] ) then
        Warn( "Redefining symbol " .. symbol );

    end
    symtab[symbol] = value;
    symCount = symCount + 1;
end

function symtab_addLabel( label )
    -- Printf( "Adding symbol : %s\n", label );
    label = label or ( '_L' .. PC_get() );
    if( label:sub(-1,-1) == ':' ) then label = label:sub(1,-2) end;
    symtab_add( label, ProgramCounter );
end

function symtab_lookup(symbol)
    return( symtab[symbol] )
end

function symtab_dump()
    Printf( "\n-------- SYMBOLS -------\n" );
    if( symCount == 0 ) then Printf("no symbols defined\n"); return; end;

    for k,v in pairs(symtab) do
        Printf("%32.32s : 0x%04x\n", k, v );
    end
end

ProgramCounter = 0;