Guest User

[CC] Chiper v7.5

a guest
Apr 13th, 2017
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 31.77 KB | None | 0 0
  1. local bckColLast = term.getBackgroundColor()
  2. local txtColLast = term.getTextColor()
  3.  
  4. local tArgs = { ... }
  5. local WIDTH, HEIGHT = term.getSize()
  6. local COLS = {
  7.     [ 0x0 ] = 0x1;
  8.     [ 0x1 ] = 0x2;
  9.     [ 0x2 ] = 0x4;
  10.     [ 0x3 ] = 0x8;
  11.     [ 0x4 ] = 0x10;
  12.     [ 0x5 ] = 0x20;
  13.     [ 0x6 ] = 0x40;
  14.     [ 0x7 ] = 0x80;
  15.     [ 0x8 ] = 0x100;
  16.     [ 0x9 ] = 0x200;
  17.     [ 0xA ] = 0x400;
  18.     [ 0xB ] = 0x800;
  19.     [ 0xC ] = 0x1000;
  20.     [ 0xD ] = 0x2000;
  21.     [ 0xE ] = 0x4000;
  22.     [ 0xF ] = 0x8000;
  23. }
  24.  
  25. local function inputStream( input )
  26.     if fs.exists( input ) then
  27.         local file = fs.open( input, 'r' )
  28.         input = file.readAll()
  29.         file.close()
  30.     else
  31.         error( "File: "..input.." does not exist!", 0 )
  32.     end
  33.     input=input.."\n"
  34.     local i = 1
  35.     local line = 1
  36.     return {
  37.         [ "next" ] = function()
  38.             local v = input:sub( i, i )
  39.             if v == "\n" then
  40.                 line = line + 1
  41.             end
  42.             i = i + 1
  43.             return v
  44.         end;
  45.         [ "peek" ] = function()
  46.             return input:sub( i, i )
  47.         end;
  48.         [ "eof" ] = function()
  49.             return i > #input
  50.         end;
  51.         [ "setPos" ] = function( pos )
  52.             i = pos
  53.         end;
  54.         [ "error" ] = function( msg )
  55.             error( tArgs[1]..":"..line..": "..msg, 0 )
  56.         end;
  57.     }
  58. end
  59.  
  60. local function tokenize( input )
  61.     local function isId( c )
  62.         return c:match( "[a-zA-Z]" ) ~= nil
  63.     end
  64.     local function isNum( c )
  65.         return c:match( "[0-9]" ) ~= nil
  66.     end
  67.     local function isPunc( c )
  68.         return c:match( "[,$#%(%).]" ) ~= nil
  69.     end
  70.     local function isWht( c )
  71.         return c:match( "[ \t\n]" ) ~= nil
  72.     end
  73.  
  74.     local function readWhile( p )
  75.         local str = ""
  76.         while input.eof() == false and p( input.peek() ) do
  77.             str = str .. input.next()
  78.         end
  79.         return str
  80.     end
  81.     local function String()
  82.         input.next()
  83.         local str = readWhile( function( p ) return p ~= "\"" end )
  84.         input.next()
  85.         return {
  86.             [ "type" ] = "str";
  87.             [ "value" ] = str;
  88.         }
  89.     end
  90.     local function Id()
  91.         return {
  92.             [ "type" ] = "id";
  93.             [ "value" ] = readWhile( function( p )
  94.                 return isNum( p ) or isId( p )
  95.             end ):upper();
  96.         }
  97.     end
  98.     local function Num()
  99.         return {
  100.             [ "type" ] = "num";
  101.             [ "value" ] = readWhile( function( p )
  102.                 return isNum( p ) or isId( p )
  103.             end );
  104.         }
  105.     end
  106.     local function Punc()
  107.         return {
  108.             [ "type" ] = "pnc";
  109.             [ "value" ] = input.next();
  110.         }
  111.     end
  112.     local function Comment()
  113.         input.next()
  114.         readWhile( function( p )
  115.             return p ~= "\n"
  116.         end )
  117.         readWhile( isWht )
  118.         if input.peek() == ";" then
  119.             Comment()
  120.         end
  121.     end
  122.  
  123.     local function Next()
  124.         readWhile( isWht )
  125.         local p = input.peek()
  126.         if p == ";" then
  127.             Comment()
  128.             p = input.peek()
  129.         end
  130.         if isNum( p ) then
  131.             return Num()
  132.         elseif isId( p ) then
  133.             return Id()
  134.         elseif isPunc( p ) then
  135.             return Punc()
  136.         elseif p == "\"" then
  137.             return String()
  138.         elseif p == "" then
  139.             return {
  140.                 [ "type" ] = "EOF";
  141.             }
  142.         elseif p == nil then
  143.             return {
  144.                 [ "type" ] = "EOF";
  145.             }
  146.         end
  147.         error( "unexpected char: "..p.." "..#p )
  148.     end
  149.  
  150.     local curr
  151.     return {
  152.         [ "next" ] = function()
  153.             local v = curr
  154.             curr = nil
  155.             return v or Next()
  156.         end;
  157.         [ "peek" ] = function()
  158.             if curr then
  159.                 return curr
  160.             else
  161.                 curr = Next()
  162.                 return curr
  163.             end
  164.         end;
  165.         [ "eof" ] = input.eof;
  166.         [ "setPos" ] = function( pos )
  167.             input.setPos( pos )
  168.             curr = nil
  169.         end;
  170.         [ "error" ] = input.error;
  171.     }
  172. end
  173.  
  174. local MNEMONICS = {
  175.     [ "HLT" ] = 0;
  176.     [ "RET" ] = 0;
  177.     [ "CALL" ] = 1;
  178.     [ "JMP" ] = 1;
  179.     [ "SHL" ] = 1;
  180.     [ "SHR" ] = 1;
  181.     [ "INC" ] = 1;
  182.     [ "DEC" ] = 1;
  183.     [ "LD" ] = 2;
  184.     [ "ADD" ] = 2;
  185.     [ "SUB" ] = 2;
  186.     [ "MUL" ] = 2;
  187.     [ "EQL" ] = 2;
  188.     [ "MEQ" ] = 2;
  189.     [ "MOR" ] = 2;
  190.     [ "LEQ" ] = 2;
  191.     [ "LES" ] = 2;
  192.     [ "INT" ] = 2;
  193.     [ "OR" ] = 2;
  194.     [ "AND" ] = 2;
  195.     [ "XOR" ] = 2;
  196. }
  197. local ACCEPTED = {}
  198. local VirtualMemory = {}
  199.  
  200. local function parse( input )
  201.     local bc = 0
  202.     local DataStart = 0xFF0
  203.     local function labelArg( name )
  204.         while input.eof() == false do
  205.             local data = input.next()
  206.             if data.type == "str" then
  207.                 if ACCEPTED[ name.value ] == nil then
  208.                     ACCEPTED[ name.value ] = DataStart-1
  209.                 end
  210.                 local d = data.value
  211.                 for i=1, #d do
  212.                     VirtualMemory[ DataStart ] = {string.byte( d:sub( i, i ) )}
  213.                     DataStart = DataStart - 1
  214.                 end
  215.             elseif data.type == "pnc" and data.value == "$" then
  216.                 if input.peek().type == "id" or input.peek().type == "num" then
  217.                     if ACCEPTED[ name.value ] == nil then
  218.                         ACCEPTED[ name.value ] = DataStart-1
  219.                     end
  220.                     VirtualMemory[ DataStart ] = {tonumber( "0x"..input.next().value )}
  221.                     DataStart = DataStart - 1
  222.                 end
  223.             elseif data.type == "pnc" and data.value == "#" then
  224.                 if input.peek().type == "id" or input.peek().type == "num" then
  225.                     if ACCEPTED[ name.value ] == nil then
  226.                         ACCEPTED[ name.value ] = DataStart-1
  227.                     end
  228.                     VirtualMemory[ DataStart ] = {tonumber( input.next().value )}
  229.                     DataStart = DataStart - 1
  230.                 end
  231.             else
  232.                 input.error("DB can be types of: String, Number!")
  233.             end
  234.             if input.peek().value == "," then
  235.                 input.next()
  236.             else
  237.                 break
  238.             end
  239.         end
  240.     end
  241.     local function parseLabels()
  242.         while input.eof() == false do
  243.             if input.peek().type == "pnc" then
  244.                 if input.peek().value == "." then
  245.                     input.next()
  246.                     local name = input.next()
  247.                     if name.type == "id" then
  248.                         if input.peek().value == "DB" then
  249.                             input.next()
  250.                             labelArg( name )
  251.                         else
  252.                             ACCEPTED[ name.value ] = bc
  253.                         end
  254.                     else
  255.                         input.error("Expected id after '.'(dot) label declaration!")
  256.                     end
  257.                 else
  258.                     input.next()
  259.                 end
  260.             else
  261.                 parseMnemonic()
  262.             end
  263.         end
  264.         input.setPos( 1 )
  265.     end
  266.  
  267.     local function Arg()
  268.         if input.peek().type == "id" then
  269.             return {
  270.                 [ "type" ] = "reg";
  271.                 [ "value" ] = input.next().value;
  272.             }
  273.         elseif input.peek().type == "pnc" then
  274.             if input.peek().value == "$" then
  275.                 input.next()
  276.                 if input.peek().type == "id" or input.peek().type == "num" then
  277.                     return {
  278.                         [ "type" ] = "num";
  279.                         [ "value" ] = tonumber( "0x"..input.next().value );
  280.                     }
  281.                 else
  282.                     input.error( "Expected hexadecimal number!" )
  283.                 end
  284.             elseif input.peek().value == "#" then
  285.                 input.next()
  286.                 if input.peek().type == "num" then
  287.                     return {
  288.                         [ "type" ] = "num";
  289.                         [ "value" ] = tonumber( input.next().value );
  290.                     }
  291.                 else
  292.                     input.error( "Expected numeric number!")
  293.                 end
  294.             elseif input.peek().value == "(" then
  295.                 input.next()
  296.                 local arg = Arg()
  297.                 if input.peek().value == ")" then
  298.                     input.next()
  299.                 else
  300.                     input.error( "Expected ')'!" )
  301.                 end
  302.                 return {
  303.                     [ "type" ] = "mem";
  304.                     [ "value" ] = arg;
  305.                 }
  306.             else
  307.                 input.error( "Expected number declaration '$' or '#' or memory location '(I)'!" )
  308.             end
  309.         else
  310.             input.error( "No valid argument!" )
  311.         end
  312.     end
  313.  
  314.     function parseMnemonic()
  315.         local p = input.peek()
  316.         if p.type == "id" then
  317.             if MNEMONICS[ p.value ] then
  318.                 p = input.next()
  319.                 local args = {}
  320.                 local size = MNEMONICS[ p.value ]
  321.                 if size > 0 then
  322.                     for i=1, size do
  323.                         args[#args+1] = Arg()
  324.                         if size > 1 then
  325.                             if i == 1 then
  326.                                 if input.peek().value == "," then
  327.                                     input.next()
  328.                                 else
  329.                                     input.error( "Expected ','(comma)" )
  330.                                 end
  331.                             end
  332.                         end
  333.                     end
  334.                 end
  335.                 bc = bc + 2
  336.                 return {
  337.                     [ "type" ] = "mnemonic";
  338.                     [ "name" ] = p.value;
  339.                     [ "args" ] = args;
  340.                 }
  341.             else
  342.                 input.error("NOT A MNEMONIC!")
  343.             end
  344.         elseif p.type == "pnc" then
  345.             if p.value == "." then
  346.                 input.next()
  347.                 input.next()
  348.                 if input.peek().value == "DB" then
  349.                     input.next()
  350.                     while input.eof() == false do
  351.                         if input.peek().value == "$" or input.peek().value == "#" then
  352.                             input.next()
  353.                         end
  354.                         input.next()
  355.                         if input.peek().value == "," then
  356.                             input.next()
  357.                         else
  358.                             break
  359.                         end
  360.                     end
  361.                 end
  362.             end
  363.         end
  364.     end
  365.     parseLabels()
  366.     local function Program()
  367.         local prog = {}
  368.         while input.eof() == false do
  369.             prog[#prog+1] = parseMnemonic()
  370.         end
  371.         return {
  372.             [ "type" ] = "program";
  373.             [ "prog" ] = prog;
  374.         }
  375.     end
  376.  
  377.     return Program()
  378. end
  379.  
  380. local function compile( input )
  381.     if input.type == "program" then
  382.         for i=1, #input.prog do
  383.             local curr = compile( input.prog[i] )
  384.             for j=1, #curr do
  385.                 VirtualMemory[ ((i*2)-2)+j ] = curr[j]
  386.             end
  387.         end
  388.     elseif input.type == "mnemonic" then
  389.         local args = {}
  390.         for i=1, #input.args do
  391.             args[#args+1] = compile( input.args[ i ] )
  392.         end
  393.         if input.name == "HLT" then
  394.             return { 0x00, 0x00 }
  395.         elseif input.name == "RET" then
  396.             return { 0x00, 0xE0 }
  397.         elseif input.name == "INC" then
  398.             if args[1].type == "reg" then
  399.                 if args[1].value == "A" then
  400.                     return { 0xA9, 0x00 }
  401.                 elseif args[1].value == "B" then
  402.                     return { 0xAA, 0x00 }
  403.                 elseif args[1].value == "I" then
  404.                     return { 0xAB, 0x00 }
  405.                 elseif args[1].value == "X" then
  406.                     return { 0xAC, 0x00 }
  407.                 else
  408.                     error( "INC compile error! Expected register!", 0 )
  409.                 end
  410.             else
  411.                 error( "INC compile error! Expected register!", 0 )
  412.             end
  413.         elseif input.name == "DEC" then
  414.             if args[1].type == "reg" then
  415.                 if args[1].value == "A" then
  416.                     return { 0xE5, 0x00 }
  417.                 elseif args[1].value == "B" then
  418.                     return { 0xE6, 0x00 }
  419.                 elseif args[1].value == "I" then
  420.                     return { 0xE7, 0x00 }
  421.                 elseif args[1].value == "X" then
  422.                     return { 0xE8, 0x00 }
  423.                 else
  424.                     error( "DEC compile error! Expected register!", 0 )
  425.                 end
  426.             else
  427.                 error( "DEC compile error! Expected register!", 0 )
  428.             end
  429.         elseif input.name == "OR" then
  430.             if args[1].type == "reg" then
  431.                 if args[1].value == "A" and args[2].type == "num" then
  432.                     return { 0xD3, args[2].value }
  433.                 elseif args[1].value == "B" and args[2].type == "num" then
  434.                     return { 0xD9, args[2].value }
  435.                 elseif args[1].value == "A" and args[2].value == "B" then
  436.                     return { 0xD4, 0x00 }
  437.                 elseif args[1].value == "B" and args[2].value == "A" then
  438.                     return { 0xDA, 0x00 }
  439.                 else
  440.                     error( "OR compile error!", 0 )
  441.                 end
  442.             else
  443.                 error( "OR compile error! Expected register!", 0 )
  444.             end
  445.         elseif input.name == "AND" then
  446.             if args[1].type == "reg" then
  447.                 if args[1].value == "A" and args[2].type == "num" then
  448.                     return { 0xD1, args[2].value }
  449.                 elseif args[1].value == "B" and args[2].type == "num" then
  450.                     return { 0xD7, args[2].value }
  451.                 elseif args[1].value == "A" and args[2].value == "B" then
  452.                     return { 0xD2, 0x00 }
  453.                 elseif args[1].value == "B" and args[2].value == "A" then
  454.                     return { 0xD8, 0x00 }
  455.                 else
  456.                     error( "AND compile error!", 0 )
  457.                 end
  458.             else
  459.                 error( "AND compile error! Expected register!", 0 )
  460.             end
  461.         elseif input.name == "XOR" then
  462.             if args[1].type == "reg" then
  463.                 if args[1].value == "A" and args[2].type == "num" then
  464.                     return { 0xD5, args[2].value }
  465.                 elseif args[1].value == "B" and args[2].type == "num" then
  466.                     return { 0xDB, args[2].value }
  467.                 elseif args[1].value == "A" and args[2].value == "B" then
  468.                     return { 0xD6, 0x00 }
  469.                 elseif args[1].value == "B" and args[2].value == "A" then
  470.                     return { 0xDC, 0x00 }
  471.                 else
  472.                     error( "XOR compile error!", 0 )
  473.                 end
  474.             else
  475.                 error( "XOR compile error! Expected register!", 0 )
  476.             end
  477.         elseif input.name == "SHL" then
  478.             if args[1].type == "reg" then
  479.                 if args[1].value == "A" then
  480.                     return { 0xE1, 0x00 }
  481.                 elseif args[1].value == "B" then
  482.                     return { 0xE2, 0x00 }
  483.                 else
  484.                     error( "XOR compile error! Expected register!", 0 )
  485.                 end
  486.             else
  487.                 error( "SHL compile error! Expected register!", 0 )
  488.             end
  489.         elseif input.name == "SHR" then
  490.             if args[1].type == "reg" then
  491.                 if args[1].value == "A" then
  492.                     return { 0xE3, 0x00 }
  493.                 elseif args[1].value == "B" then
  494.                     return { 0xE4, 0x00 }
  495.                 else
  496.                     error( "XOR compile error! Expected register!", 0 )
  497.                 end
  498.             else
  499.                 error( "SHL compile error! Expected register!", 0 )
  500.             end
  501.         elseif input.name == "CALL" then
  502.             if args[1].type == "reg" then
  503.                 if args[1].value == "I" then
  504.                     return { 0x00, 0x0E }
  505.                 elseif ACCEPTED[ args[1].value ] then
  506.                     return { 0x2, ACCEPTED[ args[1].value ] }
  507.                 else
  508.                     error( "CALL compile error! Expected 'I' or Pointer", 0 )
  509.                 end
  510.             elseif args[1].type == "num" then
  511.                 return { 0x2, args[1].value }
  512.             else
  513.                 error( "CALL Compile error!",0 )
  514.             end
  515.         elseif input.name == "INT" then
  516.             if args[1].type == "reg" then
  517.                 if args[1].value == "A" and args[2].type == "num" then
  518.                     return { 0xB, args[2].value }
  519.                 elseif args[1].value == "B" and args[2].type == "num" then
  520.                     return { 0xC, args[2].value }
  521.                 elseif args[1].value == "A" and args[2].value == "I" then
  522.                     return { 0xD0, 0x00 }
  523.                 elseif args[1].value == "B" and args[2].value == "I" then
  524.                     return { 0xE0, 0x00 }
  525.                 elseif args[1].value == "A" and ACCEPTED[ args[2].value ] then
  526.                     return { 0xB, ACCEPTED[ args[2].value ] }
  527.                 elseif args[1].value == "B" and ACCEPTED[ args[2].value ] then
  528.                     return { 0xC, ACCEPTED[ args[2].value ] }
  529.                 else
  530.                     error( "INT compile error! Expected 'I' or Pointer/Number", 0 )
  531.                 end
  532.             else
  533.                 error( "INT compile error! Expected Register A or B",0 )
  534.             end
  535.         elseif input.name == "JMP" then
  536.             if args[1].type == "num" then
  537.                 return { 0x1, args[1].value }
  538.             elseif args[1].type == "reg" then
  539.                 return { 0x1, ACCEPTED[ args[1].value ] }
  540.             else
  541.                 error( "JMP compile error! Expected 'I' or Pointer", 0 )
  542.             end
  543.         elseif input.name == "LD" then
  544.             if args[1].type == "reg" then
  545.                 if args[1].value == "A" and args[2].type == "num" then
  546.                     return { 0x30, args[2].value }
  547.                 elseif args[1].value == "B" and args[2].type == "num" then
  548.                     return { 0x40, args[2].value }
  549.                 elseif args[1].value == "B" and args[2].value == "RND" then
  550.                     return { 0x57, 0x00 }
  551.                 elseif args[1].value == "A" and args[2].type == "mem" then
  552.                     return { 0x73, 0x00 }
  553.                 elseif args[1].value == "B" and args[2].type == "mem" then
  554.                     return { 0x74, 0x00 }
  555.                 elseif args[1].value == "I" and args[2].type == "num" then
  556.                     return { 0x8, args[2].value }
  557.                 elseif args[1].value == "I" and args[2].value == "X" then
  558.                     return { 0x75, 0x00 }
  559.                 elseif args[1].value == "X" and args[2].type == "num" then
  560.                     return { 0x9, args[2].value }
  561.                 elseif args[1].value == "X" and args[2].value == "I" then
  562.                     return { 0x76, 0x00 }
  563.                 elseif args[1].value == "I" and args[2].type == "reg" then
  564.                     if ACCEPTED[ args[2].value ] then
  565.                         return { 0x8, ACCEPTED[ args[2].value ] }
  566.                     else
  567.                         error(args[2].value.." not defined!",0)
  568.                     end
  569.                 elseif args[1].value == "X" and args[2].type == "reg" then
  570.                     if ACCEPTED[ args[2].value ] then
  571.                         return { 0x9, ACCEPTED[ args[2].value ] }
  572.                     else
  573.                         error(args[2].value.." not defined!",0)
  574.                     end
  575.                 end
  576.             elseif args[1].type == "mem" and args[2].value == "A" then
  577.                 return { 0x70, 0x00 }
  578.             elseif args[1].type == "mem" and args[2].value == "B" then
  579.                 return { 0x71, 0x00 }
  580.             elseif args[1].type == "mem" and args[2].type == "num" then
  581.                 return { 0x72, args[2].value }
  582.             else
  583.                 error( "LD compile error!",0 )
  584.             end
  585.         elseif input.name == "ADD" then
  586.             if args[1].type == "reg" then
  587.                 if args[1].value == "A" and args[2].type == "num" then
  588.                     return { 0x50, args[2].value }
  589.                 elseif args[1].value == "A" and args[2].value == "B" then
  590.                     return { 0x51, 0x00 }
  591.                 elseif args[1].value == "B" and args[2].type == "num" then
  592.                     return { 0x52, args[2].value }
  593.                 elseif args[1].value == "B" and args[2].value == "A" then
  594.                     return { 0x53, 0x00 }
  595.                 elseif args[1].value == "I" and args[2].type == "num" then
  596.                     return { 0x54, args[2].value }
  597.                 elseif args[1].value == "I" and args[2].value == "A" then
  598.                     return { 0x55, 0x00 }
  599.                 elseif args[1].value == "I" and args[2].value == "B" then
  600.                     return { 0x56, 0x00 }
  601.                 elseif args[1].value == "X" and args[2].type == "num" then
  602.                     return { 0xA0, args[2].value }
  603.                 elseif args[1].value == "X" and args[2].value == "A" then
  604.                     return { 0xA1, 0x00 }
  605.                 elseif args[1].value == "X" and args[2].value == "B" then
  606.                     return { 0xA2, 0x00 }
  607.                 end
  608.             else
  609.                 error( "ADD compile error!",0 )
  610.             end
  611.         elseif input.name == "SUB" then
  612.             if args[1].type == "reg" then
  613.                 if args[1].value == "A" and args[2].type == "num" then
  614.                     return { 0x58, args[2].value }
  615.                 elseif args[1].value == "A" and args[2].value == "B" then
  616.                     return { 0x59, 0x00 }
  617.                 elseif args[1].value == "B" and args[2].type == "num" then
  618.                     return { 0x5A, args[2].value }
  619.                 elseif args[1].value == "B" and args[2].value == "A" then
  620.                     return { 0x5B, 0x00 }
  621.                 elseif args[1].value == "X" and args[2].type == "num" then
  622.                     return { 0xA3, args[2].value }
  623.                 elseif args[1].value == "X" and args[2].value == "A" then
  624.                     return { 0xA4, 0x00 }
  625.                 elseif args[1].value == "X" and args[2].value == "B" then
  626.                     return { 0xA5, 0x00 }
  627.                 end
  628.             else
  629.                 error( "SUB compile error!",0 )
  630.             end
  631.         elseif input.name == "MUL" then
  632.             if args[1].type == "reg" then
  633.                 if args[1].value == "A" and args[2].type == "num" then
  634.                     return { 0x5C, args[2].value }
  635.                 elseif args[1].value == "A" and args[2].value == "B" then
  636.                     return { 0x5D, 0x00 }
  637.                 elseif args[1].value == "B" and args[2].type == "num" then
  638.                     return { 0x5E, args[2].value }
  639.                 elseif args[1].value == "B" and args[2].value == "A" then
  640.                     return { 0x5F, 0x00 }
  641.                 elseif args[1].value == "X" and args[2].type == "num" then
  642.                     return { 0xA6, args[2].value }
  643.                 elseif args[1].value == "X" and args[2].value == "A" then
  644.                     return { 0xA7, 0x00 }
  645.                 elseif args[1].value == "X" and args[2].value == "B" then
  646.                     return { 0xA8, 0x00 }
  647.                 end
  648.             else
  649.                 error( "MUL compile error!",0 )
  650.             end
  651.         elseif input.name == "EQL" then
  652.             if args[1].type == "reg" then
  653.                 if args[1].value == "A" and args[2].value == "B" then
  654.                     return { 0x60, 0x00 }
  655.                 elseif args[1].value == "A" and args[2].type == "num" then
  656.                     return { 0x61, args[2].value }
  657.                 elseif args[1].value == "B" and args[2].type == "num" then
  658.                     return { 0x62, args[2].value }
  659.                 end
  660.             else
  661.                 error( "EQL compile error!",0 )
  662.             end
  663.         elseif input.name == "MEQ" then
  664.             if args[1].type == "reg" then
  665.                 if args[1].value == "A" and args[2].value == "B" then
  666.                     return { 0x63, 0x00 }
  667.                 elseif args[1].value == "A" and args[2].type == "num" then
  668.                     return { 0x64, args[2].value }
  669.                 elseif args[1].value == "B" and args[2].type == "num" then
  670.                     return { 0x65, args[2].value }
  671.                 end
  672.             else
  673.                 error( "MEQ compile error!",0 )
  674.             end
  675.         elseif input.name == "LEQ" then
  676.             if args[1].type == "reg" then
  677.                 if args[1].value == "RA" and args[2].value == "B" then
  678.                     return { 0x66, 0x00 }
  679.                 elseif args[1].value == "A" and args[2].type == "num" then
  680.                     return { 0x67, args[2].value }
  681.                 elseif args[1].value == "B" and args[2].type == "num" then
  682.                     return { 0x68, args[2].value }
  683.                 end
  684.             else
  685.                 error( "LEQ compile error!",0 )
  686.             end
  687.         elseif input.name == "MOR" then
  688.             if args[1].type == "reg" then
  689.                 if args[1].value == "A" and args[2].value == "B" then
  690.                     return { 0x69, 0x00 }
  691.                 elseif args[1].value == "A" and args[2].type == "num" then
  692.                     return { 0x6A, args[2].value }
  693.                 elseif args[1].value == "B" and args[2].type == "num" then
  694.                     return { 0x6B, args[2].value }
  695.                 end
  696.             else
  697.                 error( "MOR compile error!",0 )
  698.             end
  699.         elseif input.name == "LES" then
  700.             if args[1].type == "reg" then
  701.                 if args[1].value == "A" and args[2].value == "B" then
  702.                     return { 0x6C, 0x00 }
  703.                 elseif args[1].value == "A" and args[2].type == "num" then
  704.                     return { 0x6D, args[2].value }
  705.                 elseif args[1].value == "B" and args[2].type == "num" then
  706.                     return { 0x6E, args[2].value }
  707.                 end
  708.             else
  709.                 error( "LES compile error!",0 )
  710.             end
  711.         end
  712.     elseif input.type == "reg" or input.type == "num" then
  713.         return input
  714.     elseif input.type == "mem" then
  715.         return {
  716.             [ "type" ] = "mem";
  717.             [ "value" ] = input.value.value;
  718.         }
  719.     end
  720. end
  721.  
  722. local function makeByteFile( b, f )
  723.     local file = fs.open( f, 'wb' )
  724.     for i=1, 0xFFF, 2 do
  725.         if VirtualMemory[ i ] then
  726.             local b = { VirtualMemory[i] or 0, VirtualMemory[i+1] or 0 }
  727.             local byte
  728.             if b[1] == 0x8 or b[1] == 0xC or b[1] == 0xB or b[1] == 0x9 or b[1] == 0x1 or b[1] == 0x2 then
  729.                 byte = bit.bor( bit.blshift( b[1], 12 ), b[2] )
  730.             else
  731.                 if type(b[1]) == "table" then
  732.                     b[1] = b[1][1]
  733.                 end
  734.                 if type(b[2]) == "table" then
  735.                     b[2] = b[2][1]
  736.                 end
  737.                 byte = bit.bor( bit.blshift( b[1], 8 ), b[2] )
  738.             end
  739.             if byte then
  740.                 file.write( bit.brshift( bit.band( byte, 0xFF00 ), 8 ) )
  741.                 file.write( bit.band( byte, 0xFF ) )
  742.             end
  743.         else
  744.             file.write( 0 )
  745.             file.write( 0 )
  746.         end
  747.     end
  748.     file.close()
  749. end
  750.  
  751. local rPC = 0
  752. local SP = 0
  753. local rA = 0
  754. local rB = 0
  755. local rI = 0
  756. local rX = 0
  757. local HDD = {}
  758. local MEM = {}
  759. local STACK = {}
  760. local Running = false
  761. local scrBuff = {}
  762. local INTERUPTS = {}
  763. local INT = 0
  764. local TIMERS = {}
  765. local openedHDD = nil
  766.  
  767. local function createHdd()
  768.     local hdd = fs.open( "save.dat", "wb" )
  769.     for i=0, 0xFFF do
  770.         hdd.write( 0 )
  771.     end
  772.     hdd.close()
  773. end
  774.  
  775. local function readHdd()
  776.     local hdd = fs.open( "save.dat", "rb" )
  777.     for i=0, 0xFFF do
  778.         HDD[ i ] = hdd.read()
  779.     end
  780.     hdd.close()
  781. end
  782.  
  783. local function reset()
  784.     if not fs.exists( "save.dat" ) then
  785.         createHdd()
  786.     end
  787.     Running = true
  788.     for i=0, 0xFFF do
  789.         MEM[ i ] = 0
  790.     end
  791.     for i=0, 0xFFF do
  792.         HDD[ i ] = 0
  793.     end
  794.     for i=0, 0xF do
  795.         STACK[ i ] = 0
  796.     end
  797.     INT = 0
  798.     SP = 0
  799.     rPC = 0
  800.     rA = 0
  801.     rB = 0
  802.     rI = 0
  803.     rX = 0
  804.     for i=0, (WIDTH*HEIGHT)-1 do
  805.         scrBuff[ i ] = {
  806.             " ",
  807.             COLS[ 1 ],
  808.             COLS[ 0 ]
  809.         }
  810.     end
  811.     readHdd()
  812. end
  813.  
  814. local _bor, _band, _lshift, _rshift, _bxor = bit.bor, bit.band, bit.blshift, bit.brshift, bit.bxor
  815. local mrAndom = math.random
  816.  
  817. local function fetch()
  818.     local opcode = _bor( _lshift( MEM[ rPC ], 8 ), MEM[ rPC + 1 ] )
  819.     rPC = rPC + 2
  820.     return opcode
  821. end
  822.  
  823. local function wrapRegs()
  824.     rA = rA % (0xFF+1)
  825.     rB = rB % (0xFF+1)
  826.     rI = rI % (0xFFF+1)
  827.     rX = rX % (0xFFF+1)
  828.     rPC = rPC % (0xFFF+1)
  829.     if rA < 0 then rA = 0xFF end
  830.     if rB < 0 then rB = 0xFF end
  831.     if rI < 0 then rI = 0xFFF end
  832.     if rX < 0 then rX = 0xFFF end
  833. end
  834.  
  835. local tremove = table.remove
  836.  
  837. local function updateInterupts()
  838.     for i=1, #TIMERS do
  839.         local timer = TIMERS[ i ]
  840.         local time = timer.time
  841.         if time <= 0 then
  842.             STACK[ SP ] = rPC
  843.             SP = SP + 1
  844.             rPC = timer.jump
  845.             tremove( TIMERS, i )
  846.             INTERUPTS[ INT ] = {
  847.                 ['rA']=rA;
  848.                 ['rB']=rB;
  849.                 ['rI']=rI;
  850.                 ['rX']=rX;
  851.             }
  852.             INT=INT+1
  853.         else
  854.             timer.time=time-1
  855.         end
  856.     end
  857. end
  858.  
  859. local function decode( opcode )
  860.     local op = _band( opcode, 0xF000 )
  861.     if op == 0x0000 then
  862.         op = _band( opcode, 0xFF )
  863.         if op == 0x00 then
  864.             Running = false
  865.             return
  866.         elseif op == 0xE0 then
  867.             SP = SP - 1
  868.             rPC = STACK[ SP ]
  869.             if #INTERUPTS > 0 then
  870.                 INT = INT - 1
  871.                 local int = INTERUPTS[ INT ]
  872.                 rA=int.rA
  873.                 rB=int.rB
  874.                 rI=int.rI
  875.                 rX=int.rX
  876.                 INTERUPTS[ INT ] = nil
  877.             end
  878.             return
  879.         elseif op == 0x0E then
  880.             STACK[ SP ] = rPC
  881.             SP = SP + 1
  882.             rPC = rI
  883.             return
  884.         end
  885.     elseif op == 0x1000 then
  886.         rPC = _band( opcode, 0xFFF )
  887.         return
  888.     elseif op == 0x2000 then
  889.         STACK[ SP ] = rPC
  890.         SP = SP + 1
  891.         rPC = _band( opcode, 0xFFF )
  892.         return
  893.     elseif op == 0x3000 then
  894.         rA = _band( opcode, 0xFF )
  895.         return
  896.     elseif op == 0x4000 then
  897.         rB = _band( opcode, 0xFF )
  898.         return
  899.     elseif op == 0x5000 then
  900.         local hl = _band( _rshift( opcode, 8 ), 0xF )
  901.         if hl == 0x0 then
  902.             rA = rA + _band( opcode, 0xFF )
  903.             return
  904.         elseif hl == 0x1 then
  905.             rA = rA + rB
  906.             return
  907.         elseif hl == 0x2 then
  908.             rB = rB + _band( opcode, 0xFF )
  909.             return
  910.         elseif hl == 0x3 then
  911.             rB = rB + rA
  912.             return
  913.         elseif hl == 0x4 then
  914.             rI = rI + _band( opcode, 0xFF )
  915.             return
  916.         elseif hl == 0x5 then
  917.             rI = rI + rA
  918.             return
  919.         elseif hl == 0x6 then
  920.             rI = rI + rB
  921.             return
  922.         elseif hl == 0x7 then
  923.             rB = mrAndom( 0, 0xFF )
  924.             return
  925.         elseif hl == 0x8 then
  926.             rA = rA - _band( opcode, 0xFF )
  927.             return
  928.         elseif hl == 0x9 then
  929.             rA = rA - rB
  930.             return
  931.         elseif hl == 0xA then
  932.             rB = rB - _band( opcode, 0xFF )
  933.             return
  934.         elseif hl == 0xB then
  935.             rB = rB - rA
  936.             return
  937.         elseif hl == 0xC then
  938.             rA = rA * _band( opcode, 0xFF )
  939.             return
  940.         elseif hl == 0xD then
  941.             rA = rA * rB
  942.             return
  943.         elseif hl == 0xE then
  944.             rB = rB * _band( opcode, 0xFF )
  945.             return
  946.         elseif hl == 0xF then
  947.             rB = rB * rA
  948.             return
  949.         end
  950.     elseif op == 0x6000 then
  951.         local hl = _band( _rshift( opcode, 8 ), 0xF )
  952.         if hl == 0x0 then
  953.             if (rA == rB) == false then
  954.                 rPC = rPC + 2
  955.             end
  956.             return
  957.         elseif hl == 0x1 then
  958.             if (rA == _band( opcode, 0x00FF )) == false then
  959.                 rPC = rPC + 2
  960.             end
  961.             return
  962.         elseif hl == 0x2 then
  963.             if (rB == _band( opcode, 0xFF )) == false then
  964.                 rPC = rPC + 2
  965.             end
  966.             return
  967.         elseif hl == 0x3 then
  968.             if (rA >= rB) == false then
  969.                 rPC = rPC + 2
  970.             end
  971.             return
  972.         elseif hl == 0x4 then
  973.             if (rA >= _band( opcode, 0xFF )) == false then
  974.                 rPC = rPC + 2
  975.             end
  976.             return
  977.         elseif hl == 0x5 then
  978.             if (rB >= _band( opcode, 0xFF )) == false then
  979.                 rPC = rPC + 2
  980.             end
  981.             return
  982.         elseif hl == 0x6 then
  983.             if (rA <= rB) == false then
  984.                 rPC = rPC + 2
  985.             end
  986.             return
  987.         elseif hl == 0x7 then
  988.             if (rA <= _band( opcode, 0xFF )) == false then
  989.                 rPC = rPC + 2
  990.             end
  991.             return
  992.         elseif hl == 0x8 then
  993.             if (rB <= _band( opcode, 0xFF )) == false then
  994.                 rPC = rPC + 2
  995.             end
  996.             return
  997.         elseif hl == 0x9 then
  998.             if (rA > rB) == false then
  999.                 rPC = rPC + 2
  1000.             end
  1001.             return
  1002.         elseif hl == 0xA then
  1003.             if (rA > _band( opcode, 0xFF )) == false then
  1004.                 rPC = rPC + 2
  1005.             end
  1006.             return
  1007.         elseif hl == 0xB then
  1008.             if (rB > _band( opcode, 0xFF )) == false then
  1009.                 rPC = rPC + 2
  1010.             end
  1011.             return
  1012.         elseif hl == 0xC then
  1013.             if (rA < rB) == false then
  1014.                 rPC = rPC + 2
  1015.             end
  1016.             return
  1017.         elseif hl == 0xD then
  1018.             if (rA < _band( opcode, 0xFF )) == false then
  1019.                 rPC = rPC + 2
  1020.             end
  1021.             return
  1022.         elseif hl == 0xE then
  1023.             if (rB < _band( opcode, 0xFF )) == false then
  1024.                 rPC = rPC + 2
  1025.             end
  1026.             return
  1027.         end
  1028.     elseif op == 0x7000 then
  1029.         local hl = _band( _rshift( opcode, 8 ), 0xF )
  1030.         if hl == 0 then
  1031.             MEM[ rI ] = rA
  1032.             return
  1033.         elseif hl == 1 then
  1034.             MEM[ rI ] = rB
  1035.             return
  1036.         elseif hl == 2 then
  1037.             MEM[ rI ] = _band( opcode, 0xFF )
  1038.             return
  1039.         elseif hl == 3 then
  1040.             rA = MEM[ rI ]
  1041.             return
  1042.         elseif hl == 4 then
  1043.             rB = MEM[ rI ]
  1044.             return
  1045.         elseif hl == 5 then
  1046.             rX = rI
  1047.             return
  1048.         elseif hl == 6 then
  1049.             rI = rX
  1050.             return
  1051.         end
  1052.     elseif op == 0x8000 then
  1053.         rI = _band( opcode, 0xFFF )
  1054.         return
  1055.     elseif op == 0x9000 then
  1056.         rX = _band( opcode, 0xFFF )
  1057.         return
  1058.     elseif op == 0xA000 then
  1059.         local hl = _band( _rshift( opcode, 8 ), 0xF )
  1060.         if hl == 0x0 then
  1061.             rX = rX + _band( opcode, 0xFF )
  1062.             return
  1063.         elseif hl == 0x1 then
  1064.             rX = rX + rA
  1065.             return
  1066.         elseif hl == 0x2 then
  1067.             rX = rX + rB
  1068.             return
  1069.         elseif hl == 0x3 then
  1070.             rX = rX - _band( opcode, 0xFF )
  1071.             return
  1072.         elseif hl == 0x4 then
  1073.             rX = rX - rA
  1074.             return
  1075.         elseif hl == 0x5 then
  1076.             rX = rX - rB
  1077.             return
  1078.         elseif hl == 0x6 then
  1079.             rX = rX * _band( opcode, 0xFF )
  1080.             return
  1081.         elseif hl == 0x7 then
  1082.             rX = rX * rA
  1083.             return
  1084.         elseif hl == 0x8 then
  1085.             rX = rX * rB
  1086.             return
  1087.         elseif hl == 0x9 then
  1088.             rA = rA + 1
  1089.         elseif hl == 0xA then
  1090.             rB = rB + 1
  1091.         elseif hl == 0xB then
  1092.             rI = rI + 1
  1093.         elseif hl == 0xC then
  1094.             rX = rX + 1
  1095.         end
  1096.     elseif op == 0xB000 then
  1097.         TIMERS[ #TIMERS + 1 ] = {
  1098.             ["time"] = rA;
  1099.             ["jump"] = _band( opcode, 0xFF );
  1100.         }
  1101.         return
  1102.     elseif op == 0xC000 then
  1103.         TIMERS[ #TIMERS + 1 ] = {
  1104.             ["time"] = rB;
  1105.             ["jump"] = _band( opcode, 0xFF );
  1106.         }
  1107.         return
  1108.     elseif op == 0xD000 then
  1109.         local hl = _band( _rshift( opcode, 8 ), 0xF )
  1110.         if hl == 0x0 then
  1111.             TIMERS[ #TIMERS + 1 ] = {
  1112.                 ["time"] = rA;
  1113.                 ["jump"] = rI;
  1114.             }
  1115.             return
  1116.         elseif hl == 0x1 then
  1117.             rA = _band( rA, _band( opcode, 0xFF ) )
  1118.             return
  1119.         elseif hl == 0x2 then
  1120.             rA = _band( rA, rB )
  1121.             return
  1122.         elseif hl == 0x3 then
  1123.             rA = _bor( rA, _band( opcode, 0xFF ) )
  1124.             return
  1125.         elseif hl == 0x4 then
  1126.             rA = _bor( rA, rB )
  1127.             return
  1128.         elseif hl == 0x5 then
  1129.             rA = _bxor( rA, _band( opcode, 0xFF ) )
  1130.             return
  1131.         elseif hl == 0x6 then
  1132.             rA = _bxor( rA, rB )
  1133.             return
  1134.         elseif hl == 0x7 then
  1135.             rB = _band( rB, _band( opcode, 0xFF ) )
  1136.             return
  1137.         elseif hl == 0x8 then
  1138.             rB = _band( rB, rA )
  1139.             return
  1140.         elseif hl == 0x9 then
  1141.             rB = _bor( rB, _band( opcode, 0xFF ) )
  1142.             return
  1143.         elseif hl == 0xA then
  1144.             rB = _bor( rB, rA )
  1145.             return
  1146.         elseif hl == 0xB then
  1147.             rB = _bxor( rB, _band( opcode, 0xFF ) )
  1148.             return
  1149.         elseif hl == 0xC then
  1150.             rB = _bxor( rB, rA )
  1151.             return
  1152.         end
  1153.     elseif op == 0xE000 then
  1154.         local hl = _band( _rshift( opcode, 8 ), 0xF )
  1155.         if hl == 0x0 then
  1156.             TIMERS[ #TIMERS + 1 ] = {
  1157.                 ["time"] = rB;
  1158.                 ["jump"] = rI;
  1159.             }
  1160.             return
  1161.         elseif hl == 0x1 then
  1162.             rA = _lshift( rA, 1 )
  1163.             return
  1164.         elseif hl == 0x2 then
  1165.             rB = _lshift( rB, 1 )
  1166.             return
  1167.         elseif hl == 0x3 then
  1168.             rA = _rshift( rA, 1 )
  1169.             return
  1170.         elseif hl == 0x4 then
  1171.             rB = _rshift( rB, 1 )
  1172.             return
  1173.         elseif hl == 0x5 then
  1174.             rA = rA - 1
  1175.         elseif hl == 0x6 then
  1176.             rB = rB - 1
  1177.         elseif hl == 0x7 then
  1178.             rI = rI - 1
  1179.         elseif hl == 0x8 then
  1180.             rX = rX - 1
  1181.         end
  1182.     end
  1183. end
  1184.  
  1185. local function loadROM( fn )
  1186.     printError( "LOADING..." )
  1187.     local file = fs.open( fn, "rb" )
  1188.     local byte = file.read()
  1189.     local i = 0
  1190.     while byte ~= nil do
  1191.         MEM[ i ] = byte
  1192.         i = i + 1
  1193.         byte = file.read()
  1194.     end
  1195.     file.close()
  1196.     term.setTextColor( colors.green )
  1197.     print( "LOADED: '"..fn.."'!" )
  1198. end
  1199. local toChar = string.char
  1200. local mFloor = math.floor
  1201. local tSetPos = term.setCursorPos
  1202. local tTSetCol = term.setTextColor
  1203. local tBSetCol = term.setBackgroundColor
  1204. local tWrite = term.write
  1205. local osPull = coroutine.yield
  1206. local osQueue = os.queueEvent
  1207. local BUFFSIZE=(WIDTH*HEIGHT)-1
  1208. local function cycle()
  1209.     for i=1, 0x26 do
  1210.         decode( fetch() )
  1211.         if Running == false then break end
  1212.         wrapRegs()
  1213.         updateInterupts()
  1214.         scrBuff[ ((MEM[ 0xFFB ]*WIDTH) + MEM[ 0xFFA ]) % BUFFSIZE ] = {
  1215.             toChar( MEM[ 0xFFE ] ),
  1216.             COLS[ MEM[ 0xFFD ] ],
  1217.             COLS[ MEM[ 0xFFC ] ]
  1218.         }
  1219.         if MEM[ 0xFFF ] == 1 then
  1220.             MEM[ 0xFFF ] = 0
  1221.             for i=0, BUFFSIZE do
  1222.                 tSetPos( 1+(i % WIDTH), 1+mFloor( i / WIDTH ) )
  1223.                 tBSetCol( scrBuff[ i ][ 3 ] )
  1224.                 tTSetCol( scrBuff[ i ][ 2 ] )
  1225.                 tWrite( scrBuff[ i ][ 1 ] )
  1226.             end
  1227.         elseif MEM[ 0xFFF ] == 2 then
  1228.             MEM[ 0xFFF ] = 0
  1229.             local buff = scrBuff[ ((MEM[ 0xFFB ]*WIDTH) + MEM[ 0xFFA ]) % BUFFSIZE ]
  1230.             MEM[ 0xFFE ] = buff[ 1 ]
  1231.             MEM[ 0xFFD ] = buff[ 2 ]
  1232.             MEM[ 0xFFC ] = buff[ 3 ]
  1233.         elseif MEM[ 0xFFF ] == 3 then
  1234.             MEM[ 0xFFF ] = 0
  1235.             HDD[ rX ] = MEM[ 0xFF8 ]
  1236.         elseif MEM[ 0xFFF ] == 4 then
  1237.             MEM[ 0xFFF ] = 0
  1238.             MEM[ 0xFF8 ] = HDD[ rX ]
  1239.         end
  1240.     end
  1241. end
  1242.  
  1243. local KEYS = {
  1244.     -- P1
  1245.     [ keys.up ] = 0x1; -- UP
  1246.     [ keys.down ] = 0x2; -- DOWN
  1247.     [ keys.left ] = 0x3; -- LEFT
  1248.     [ keys.right ] = 0x4; -- RIGHT
  1249.     [  keys.a ] = 0x5; -- A
  1250.     [  keys.b ] = 0x6; -- B
  1251.  
  1252.     -- P2
  1253.     [  72 ] = 0x7; -- UP
  1254.     [  76 ] = 0x8; -- DOWN
  1255.     [  75 ] = 0x9; -- LEFT
  1256.     [  77 ] = 0xA; -- RIGHT
  1257.     [  51 ] = 0xB; -- A
  1258.     [  52 ] = 0xC; -- B
  1259. }
  1260.  
  1261. if #tArgs > 1 then
  1262.     makeByteFile( compile( parse( tokenize( inputStream( tArgs[ 1 ] ) ) ) ), tArgs[ 2 ] )
  1263.     term.setTextColor( colors.green )
  1264.     print( "COMPILED!" )
  1265.     return
  1266. end
  1267. reset()
  1268. loadROM( tArgs[ 1 ] )
  1269. local myTimer = os.startTimer( 0.01 )
  1270. while Running do
  1271.     local ev, val = osPull()
  1272.     if ev == "key" then
  1273.         MEM[ 0xFF9 ] = (KEYS[ val ] or 0)
  1274.     elseif ev == "key_up" then
  1275.         MEM[ 0xFF9 ] = 0
  1276.     elseif ev == "timer" then
  1277.         if val == myTimer then
  1278.             cycle()
  1279.             myTimer = os.startTimer( 0.01 )
  1280.         end
  1281.     elseif ev == "terminate" then
  1282.         cycle()
  1283.         break
  1284.     end
  1285. end
  1286. term.setBackgroundColor( bckColLast )
  1287. term.setTextColor( txtColLast )
  1288. term.setCursorPos( 1, 1 )
  1289. term.clear()
  1290. fs.delete( "save.dat" )
  1291. local hdd = fs.open( "save.dat", "wb" )
  1292. for i=0, 0xFFF do
  1293.     hdd.write( HDD[ i ] )
  1294. end
  1295. hdd.close()
  1296. print( "Chiper stopped." )
Advertisement
Add Comment
Please, Sign In to add comment