Guest User

[CC] Chiper

a guest
Mar 29th, 2017
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 22.38 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..","
  34.     local i = 1
  35.     return {
  36.         [ "next" ] = function()
  37.             local v = input:sub( i, i )
  38.             i = i + 1
  39.             return v
  40.         end;
  41.         [ "peek" ] = function()
  42.             return input:sub( i, i )
  43.         end;
  44.         [ "eof" ] = function()
  45.             return i > #input
  46.         end;
  47.         [ "setPos" ] = function( pos )
  48.             i = pos
  49.         end
  50.     }
  51. end
  52.  
  53. local function tokenize( input )
  54.     local function isId( c )
  55.         return c:match( "[a-zA-Z]" ) ~= nil
  56.     end
  57.     local function isNum( c )
  58.         return c:match( "[0-9]" ) ~= nil
  59.     end
  60.     local function isPunc( c )
  61.         return c:match( "[,$#%(%).]" ) ~= nil
  62.     end
  63.     local function isWht( c )
  64.         return c:match( "[ \t\n]" ) ~= nil
  65.     end
  66.  
  67.     local function readWhile( p )
  68.         local str = ""
  69.         while input.eof() == false and p( input.peek() ) do
  70.             str = str .. input.next()
  71.         end
  72.         return str
  73.     end
  74.     local function String()
  75.         input.next()
  76.         local str = readWhile( function( p ) return p ~= "\"" end )
  77.         input.next()
  78.         return {
  79.             [ "type" ] = "str";
  80.             [ "value" ] = str;
  81.         }
  82.     end
  83.     local function Id()
  84.         return {
  85.             [ "type" ] = "id";
  86.             [ "value" ] = readWhile( function( p )
  87.                 return isNum( p ) or isId( p )
  88.             end ):upper();
  89.         }
  90.     end
  91.     local function Num()
  92.         return {
  93.             [ "type" ] = "num";
  94.             [ "value" ] = readWhile( function( p )
  95.                 return isNum( p ) or isId( p )
  96.             end );
  97.         }
  98.     end
  99.     local function Punc()
  100.         return {
  101.             [ "type" ] = "pnc";
  102.             [ "value" ] = input.next();
  103.         }
  104.     end
  105.     local function Comment()
  106.         input.next()
  107.         readWhile( function( p )
  108.             return p ~= "\n"
  109.         end )
  110.         readWhile( isWht )
  111.         if input.peek() == ";" then
  112.             Comment()
  113.         end
  114.     end
  115.  
  116.     local function Next()
  117.         readWhile( isWht )
  118.         local p = input.peek()
  119.         if p == ";" then
  120.             Comment()
  121.             p = input.peek()
  122.         end
  123.         if isNum( p ) then
  124.             return Num()
  125.         elseif isId( p ) then
  126.             return Id()
  127.         elseif isPunc( p ) then
  128.             return Punc()
  129.         elseif p == "" then
  130.             return {
  131.                 [ "type" ] = "EOF";
  132.             }
  133.         elseif p == "\"" then
  134.             return String()
  135.         end
  136.         error( "unexpected char: "..p.." "..#p )
  137.     end
  138.  
  139.     local curr
  140.     return {
  141.         [ "next" ] = function()
  142.             local v = curr
  143.             curr = nil
  144.             return v or Next()
  145.         end;
  146.         [ "peek" ] = function()
  147.             if curr then
  148.                 return curr
  149.             else
  150.                 curr = Next()
  151.                 return curr
  152.             end
  153.         end;
  154.         [ "eof" ] = input.eof;
  155.         [ "setPos" ] = function( pos )
  156.             input.setPos( pos )
  157.             curr = nil
  158.         end;
  159.     }
  160. end
  161.  
  162. local MNEMONICS = {
  163.     [ "HLT" ] = 0;
  164.     [ "RET" ] = 0;
  165.     [ "CALL" ] = 1;
  166.     [ "JMP" ] = 1;
  167.     [ "LD" ] = 2;
  168.     [ "ADD" ] = 2;
  169.     [ "SUB" ] = 2;
  170.     [ "MUL" ] = 2;
  171.     [ "EQL" ] = 2;
  172.     [ "MEQ" ] = 2;
  173.     [ "MOR" ] = 2;
  174.     [ "LEQ" ] = 2;
  175.     [ "LES" ] = 2;
  176. }
  177. local ACCEPTED = {}
  178. local VirtualMemory = {}
  179.  
  180. local function parse( input )
  181.     local bc = 0
  182.     local DataStart = 0xFF0
  183.     local function parseLabels()
  184.         while input.eof() == false do
  185.             if input.peek().type == "pnc" then
  186.                 if input.peek().value == "." then
  187.                     input.next()
  188.                     local name = input.next()
  189.                     if name.type == "id" then
  190.                         if input.peek().value == "DB" then
  191.                             input.next()
  192.                             local data = input.next()
  193.                             if data.type == "str" then
  194.                                 ACCEPTED[ name.value ] = DataStart-1
  195.                                 local d = data.value
  196.                                 for i=1, #d do
  197.                                     VirtualMemory[ DataStart ] = string.byte( d:sub( i, i ) )
  198.                                     DataStart = DataStart - 1
  199.                                 end
  200.                                 VirtualMemory[ DataStart ] = 0
  201.                                 DataStart = DataStart - 1
  202.                             elseif data.type == "pnc" and data.value == "$" then
  203.                                 if input.peek().type == "id" or input.peek().type == "num" then
  204.                                     ACCEPTED[ name.value ] = DataStart-1
  205.                                     VirtualMemory[ DataStart ] = tonumber( "0x"..input.next().value )
  206.                                     DataStart = DataStart - 1
  207.                                     VirtualMemory[ DataStart ] = 0
  208.                                 end
  209.                             elseif data.type == "pnc" and data.value == "#" then
  210.                                 if input.peek().type == "id" or input.peek().type == "num" then
  211.                                     ACCEPTED[ name.value ] = DataStart-1
  212.                                     VirtualMemory[ DataStart ] = tonumber( input.next().value )
  213.                                     DataStart = DataStart - 1
  214.                                     VirtualMemory[ DataStart ] = 0
  215.                                 end
  216.                             else
  217.                                 error("Unacceptable!")
  218.                             end
  219.                         else
  220.                             ACCEPTED[ name.value ] = bc
  221.                         end
  222.                     else
  223.                         error("Unacceptable!")
  224.                     end
  225.                 else
  226.                     input.next()
  227.                 end
  228.             else
  229.                 parseMnemonic()
  230.             end
  231.         end
  232.         input.setPos( 1 )
  233.     end
  234.  
  235.     local function Arg()
  236.         if input.peek().type == "id" then
  237.             return {
  238.                 [ "type" ] = "reg";
  239.                 [ "value" ] = input.next().value;
  240.             }
  241.         elseif input.peek().type == "pnc" then
  242.             if input.peek().value == "$" then
  243.                 input.next()
  244.                 if input.peek().type == "id" or input.peek().type == "num" then
  245.                     return {
  246.                         [ "type" ] = "num";
  247.                         [ "value" ] = tonumber( "0x"..input.next().value );
  248.                     }
  249.                 else
  250.                     error( "NO NUM! Unacceptable!" )
  251.                 end
  252.             elseif input.peek().value == "#" then
  253.                 input.next()
  254.                 if input.peek().type == "num" then
  255.                     return {
  256.                         [ "type" ] = "num";
  257.                         [ "value" ] = tonumber( input.next().value );
  258.                     }
  259.                 else
  260.                     error( "NO NUM! Unacceptable!" )
  261.                 end
  262.             elseif input.peek().value == "(" then
  263.                 input.next()
  264.                 local arg = Arg()
  265.                 if input.peek().value == ")" then
  266.                     input.next()
  267.                 else
  268.                     error( "NO CLOSE! Unacceptable!" )
  269.                 end
  270.                 return {
  271.                     [ "type" ] = "mem";
  272.                     [ "value" ] = arg;
  273.                 }
  274.             else
  275.                 error( "NO NUM! Unacceptable!" )
  276.             end
  277.         else
  278.             error( "NO OP! Unacceptable!" )
  279.         end
  280.     end
  281.  
  282.     function parseMnemonic()
  283.         local p = input.peek()
  284.         if p.type == "id" then
  285.             if MNEMONICS[ p.value ] then
  286.                 p = input.next()
  287.                 local args = {}
  288.                 local size = MNEMONICS[ p.value ]
  289.                 if size > 0 then
  290.                     for i=1, size do
  291.                         args[#args+1] = Arg()
  292.                         if input.peek().value == "," then
  293.                             input.next()
  294.                         end
  295.                     end
  296.                 end
  297.                 bc = bc + 2
  298.                 return {
  299.                     [ "type" ] = "mnemonic";
  300.                     [ "name" ] = p.value;
  301.                     [ "args" ] = args;
  302.                 }
  303.             else
  304.                 error("NOT A MNEMONIC! Unacceptable! "..p.value)
  305.             end
  306.         elseif p.type == "pnc" then
  307.             if p.value == "." then
  308.                 input.next()
  309.                 input.next()
  310.                 if input.peek().value == "DB" then
  311.                     input.next()
  312.                     local _ = input.next()
  313.                     if _.value == "$" or _.value == "#" then
  314.                         input.next()
  315.                     end
  316.                 end
  317.             end
  318.         end
  319.     end
  320.     parseLabels()
  321.     local function Program()
  322.         local prog = {}
  323.         while input.eof() == false do
  324.             prog[#prog+1] = parseMnemonic()
  325.         end
  326.         return {
  327.             [ "type" ] = "program";
  328.             [ "prog" ] = prog;
  329.         }
  330.     end
  331.  
  332.     return Program()
  333. end
  334.  
  335. local function compile( input )
  336.     if input.type == "program" then
  337.         for i=1, #input.prog do
  338.             local curr = compile( input.prog[i] )
  339.             for j=1, #curr do
  340.                 VirtualMemory[ ((i*2)-2)+j ] = curr[j]
  341.             end
  342.         end
  343.     elseif input.type == "mnemonic" then
  344.         local args = {}
  345.         for i=1, #input.args do
  346.             args[#args+1] = compile( input.args[ i ] )
  347.         end
  348.         if input.name == "HLT" then
  349.             return { 0x00, 0x00 }
  350.         elseif input.name == "RET" then
  351.             return { 0x00, 0xE0 }
  352.         elseif input.name == "CALL" then
  353.             if args[1].type == "reg" then
  354.                 if args[1].value == "I" then
  355.                     return { 0x00, 0x0E }
  356.                 elseif ACCEPTED[ args[1].value ] then
  357.                     return { 0x2, ACCEPTED[ args[1].value ] }
  358.                 else
  359.                     error( "REG ERR" )
  360.                 end
  361.             elseif args[1].type == "num" then
  362.                 return { 0x2, args[1].value }
  363.             end
  364.         elseif input.name == "JMP" then
  365.             if args[1].type == "num" then
  366.                 return { 0x1, args[1].value }
  367.             elseif args[1].type == "reg" then
  368.                 return { 0x1, ACCEPTED[ args[1].value ] }
  369.             end
  370.         elseif input.name == "LD" then
  371.             if args[1].type == "reg" then
  372.                 if args[1].value == "A" and args[2].type == "num" then
  373.                     return { 0x30, args[2].value }
  374.                 elseif args[1].value == "B" and args[2].type == "num" then
  375.                     return { 0x40, args[2].value }
  376.                 elseif args[1].value == "B" and args[2].value == "RND" then
  377.                     return { 0x57, 0x00 }
  378.                 elseif args[1].value == "A" and args[2].type == "mem" then
  379.                     return { 0x73, 0x00 }
  380.                 elseif args[1].value == "B" and args[2].type == "mem" then
  381.                     return { 0x74, 0x00 }
  382.                 elseif args[1].value == "I" and args[2].type == "num" then
  383.                     return { 0x8, args[2].value }
  384.                 elseif args[1].value == "I" and args[2].value == "X" then
  385.                     return { 0x75, 0x00 }
  386.                 elseif args[1].value == "X" and args[2].type == "num" then
  387.                     return { 0x9, args[2].value }
  388.                 elseif args[1].value == "X" and args[2].value == "I" then
  389.                     return { 0x76, 0x00 }
  390.                 elseif args[1].value == "I" and args[2].type == "reg" then
  391.                     if ACCEPTED[ args[2].value ] then
  392.                         return { 0x8, ACCEPTED[ args[2].value ] }
  393.                     else
  394.                         error("Unacceptable!")
  395.                     end
  396.                 elseif args[1].value == "X" and args[2].type == "reg" then
  397.                     if ACCEPTED[ args[2].value ] then
  398.                         return { 0x9, ACCEPTED[ args[2].value ] }
  399.                     else
  400.                         error("Unacceptable!")
  401.                     end
  402.                 end
  403.             elseif args[1].type == "mem" and args[2].value == "A" then
  404.                 return { 0x70, 0x00 }
  405.             elseif args[1].type == "mem" and args[2].value == "B" then
  406.                 return { 0x71, 0x00 }
  407.             elseif args[1].type == "mem" and args[2].type == "num" then
  408.                 return { 0x72, args[2].value }
  409.             end
  410.         elseif input.name == "ADD" then
  411.             if args[1].type == "reg" then
  412.                 if args[1].value == "A" and args[2].type == "num" then
  413.                     return { 0x50, args[2].value }
  414.                 elseif args[1].value == "A" and args[2].value == "B" then
  415.                     return { 0x51, 0x00 }
  416.                 elseif args[1].value == "B" and args[2].type == "num" then
  417.                     return { 0x52, args[2].value }
  418.                 elseif args[1].value == "B" and args[2].value == "A" then
  419.                     return { 0x53, 0x00 }
  420.                 elseif args[1].value == "I" and args[2].type == "num" then
  421.                     return { 0x54, args[2].value }
  422.                 elseif args[1].value == "I" and args[2].value == "A" then
  423.                     return { 0x55, 0x00 }
  424.                 elseif args[1].value == "I" and args[2].value == "B" then
  425.                     return { 0x56, 0x00 }
  426.                 elseif args[1].value == "X" and args[2].type == "num" then
  427.                     return { 0xA0, args[2].value }
  428.                 elseif args[1].value == "X" and args[2].value == "A" then
  429.                     return { 0xA1, 0x00 }
  430.                 elseif args[1].value == "X" and args[2].value == "B" then
  431.                     return { 0xA2, 0x00 }
  432.                 end
  433.             end
  434.         elseif input.name == "SUB" then
  435.             if args[1].type == "reg" then
  436.                 if args[1].value == "A" and args[2].type == "num" then
  437.                     return { 0x58, args[2].value }
  438.                 elseif args[1].value == "A" and args[2].value == "B" then
  439.                     return { 0x59, 0x00 }
  440.                 elseif args[1].value == "B" and args[2].type == "num" then
  441.                     return { 0x5A, args[2].value }
  442.                 elseif args[1].value == "B" and args[2].value == "A" then
  443.                     return { 0x5B, 0x00 }
  444.                 elseif args[1].value == "X" and args[2].type == "num" then
  445.                     return { 0xA3, args[2].value }
  446.                 elseif args[1].value == "X" and args[2].value == "A" then
  447.                     return { 0xA4, 0x00 }
  448.                 elseif args[1].value == "X" and args[2].value == "B" then
  449.                     return { 0xA5, 0x00 }
  450.                 end
  451.             end
  452.         elseif input.name == "MUL" then
  453.             if args[1].type == "reg" then
  454.                 if args[1].value == "A" and args[2].type == "num" then
  455.                     return { 0x5C, args[2].value }
  456.                 elseif args[1].value == "A" and args[2].value == "B" then
  457.                     return { 0x5D, 0x00 }
  458.                 elseif args[1].value == "B" and args[2].type == "num" then
  459.                     return { 0x5E, args[2].value }
  460.                 elseif args[1].value == "B" and args[2].value == "A" then
  461.                     return { 0x5F, 0x00 }
  462.                 elseif args[1].value == "X" and args[2].type == "num" then
  463.                     return { 0xA6, args[2].value }
  464.                 elseif args[1].value == "X" and args[2].value == "A" then
  465.                     return { 0xA7, 0x00 }
  466.                 elseif args[1].value == "X" and args[2].value == "B" then
  467.                     return { 0xA8, 0x00 }
  468.                 end
  469.             end
  470.         elseif input.name == "EQL" then
  471.             if args[1].type == "reg" then
  472.                 if args[1].value == "A" and args[2].value == "B" then
  473.                     return { 0x60, 0x00 }
  474.                 elseif args[1].value == "A" and args[2].type == "num" then
  475.                     return { 0x61, args[2].value }
  476.                 elseif args[1].value == "B" and args[2].type == "num" then
  477.                     return { 0x61, args[2].value }
  478.                 end
  479.             end
  480.         elseif input.name == "MEQ" then
  481.             if args[1].type == "reg" then
  482.                 if args[1].value == "A" and args[2].value == "B" then
  483.                     return { 0x63, 0x00 }
  484.                 elseif args[1].value == "A" and args[2].type == "num" then
  485.                     return { 0x64, args[2].value }
  486.                 elseif args[1].value == "B" and args[2].type == "num" then
  487.                     return { 0x65, args[2].value }
  488.                 end
  489.             end
  490.         elseif input.name == "LEQ" then
  491.             if args[1].type == "reg" then
  492.                 if args[1].value == "RA" and args[2].value == "B" then
  493.                     return { 0x66, 0x00 }
  494.                 elseif args[1].value == "A" and args[2].type == "num" then
  495.                     return { 0x67, args[2].value }
  496.                 elseif args[1].value == "B" and args[2].type == "num" then
  497.                     return { 0x68, args[2].value }
  498.                 end
  499.             end
  500.         elseif input.name == "MOR" then
  501.             if args[1].type == "reg" then
  502.                 if args[1].value == "A" and args[2].value == "B" then
  503.                     return { 0x69, 0x00 }
  504.                 elseif args[1].value == "A" and args[2].type == "num" then
  505.                     return { 0x6A, args[2].value }
  506.                 elseif args[1].value == "B" and args[2].type == "num" then
  507.                     return { 0x6B, args[2].value }
  508.                 end
  509.             end
  510.         elseif input.name == "LES" then
  511.             if args[1].type == "reg" then
  512.                 if args[1].value == "A" and args[2].value == "B" then
  513.                     return { 0x6C, 0x00 }
  514.                 elseif args[1].value == "A" and args[2].type == "num" then
  515.                     return { 0x6D, args[2].value }
  516.                 elseif args[1].value == "B" and args[2].type == "num" then
  517.                     return { 0x6E, args[2].value }
  518.                 end
  519.             end
  520.         end
  521.     elseif input.type == "reg" or input.type == "num" then
  522.         return input
  523.     elseif input.type == "mem" then
  524.         return {
  525.             [ "type" ] = "mem";
  526.             [ "value" ] = input.value.value;
  527.         }
  528.     end
  529. end
  530.  
  531. local function makeByteFile( b, f )
  532.     local file = fs.open( f, 'wb' )
  533.     for i=1, 0xFFF-1, 2 do
  534.         if VirtualMemory[ i ] then
  535.             local b = { VirtualMemory[i] or 0, VirtualMemory[i+1] or 0 }
  536.             local byte
  537.             if b[1] == 0x8 or b[1] == 0x9 or b[1] == 0x1 or b[1] == 0x2 then
  538.                 byte = bit.bor( bit.blshift( b[1], 12 ), b[2] )
  539.             else
  540.                 byte = bit.bor( bit.blshift( b[1], 8 ), b[2] )
  541.             end
  542.             if byte then
  543.                 file.write( bit.brshift( bit.band( byte, 0xFF00 ), 8 ) )
  544.                 file.write( bit.band( byte, 0xFF ) )
  545.             end
  546.         else
  547.             file.write( 0 )
  548.             file.write( 0 )
  549.         end
  550.     end
  551.     file.close()
  552. end
  553.  
  554. local rPC = 0
  555. local SP = 0
  556. local rA = 0
  557. local rB = 0
  558. local rI = 0
  559. local rX = 0
  560. local MEM = {}
  561. local STACK = {}
  562. local Running = false
  563. local scrBuff = {}
  564.  
  565. local function reset()
  566.     Running = true
  567.     for i=0, 0xFFF do
  568.         MEM[ i ] = 0
  569.     end
  570.     for i=0, 0xF do
  571.         STACK[ i ] = 0
  572.     end
  573.     SP = 0
  574.     rPC = 0
  575.     rA = 0
  576.     rB = 0
  577.     rI = 0
  578.     rX = 0
  579.     for i=0, (WIDTH*HEIGHT)-1 do
  580.         scrBuff[ i ] = {
  581.             " ",
  582.             COLS[ 1 ],
  583.             COLS[ 0 ]
  584.         }
  585.     end
  586. end
  587.  
  588. local _bor, _band, _lshift, _rshift = bit.bor, bit.band, bit.blshift, bit.brshift
  589. local mrAndom = math.random
  590.  
  591. local function fetch()
  592.     local opcode = _bor( _lshift( MEM[ rPC ], 8 ), MEM[ rPC + 1 ] )
  593.     rPC = rPC + 2
  594.     return opcode
  595. end
  596.  
  597. local function wrapRegs()
  598.     if rA < 0 then rA = 0xFF end
  599.     rA = rA % (0xFF + 1)
  600.     if rB < 0 then rB = 0xFF end
  601.     rB = rB % (0xFF + 1)
  602.     if rI < 0 then rI = 0xFFF end
  603.     rI = rI % (0xFFF + 1)
  604.     if rX < 0 then rX = 0xFFF end
  605.     rX = rX % (0xFFF + 1)
  606.     rPC = rPC % (0xFFF + 1)
  607. end
  608.  
  609. local function decode( opcode )
  610.     local op = _band( opcode, 0xF000 )
  611.     if op == 0x0000 then
  612.         op = _band( opcode, 0xFF )
  613.         if op == 0x00 then
  614.             Running = false
  615.             return
  616.         elseif op == 0xE0 then
  617.             SP = SP - 1
  618.             rPC = STACK[ SP ]
  619.             return
  620.         elseif op == 0x0E then
  621.             STACK[ SP ] = rPC
  622.             SP = SP + 1
  623.             rPC = rI
  624.             return
  625.         end
  626.     elseif op == 0x1000 then
  627.         rPC = _band( opcode, 0xFFF )
  628.         return
  629.     elseif op == 0x2000 then
  630.         STACK[ SP ] = rPC
  631.         SP = SP + 1
  632.         rPC = _band( opcode, 0xFFF )
  633.         return
  634.     elseif op == 0x3000 then
  635.         rA = _band( opcode, 0xFF )
  636.         return
  637.     elseif op == 0x4000 then
  638.         rB = _band( opcode, 0xFF )
  639.         return
  640.     elseif op == 0x5000 then
  641.         local hl = _band( _rshift( opcode, 8 ), 0xF )
  642.         if hl == 0x0 then
  643.             rA = rA + _band( opcode, 0xFF )
  644.             return
  645.         elseif hl == 0x1 then
  646.             rA = rA + rB
  647.             return
  648.         elseif hl == 0x2 then
  649.             rB = rB + _band( opcode, 0xFF )
  650.             return
  651.         elseif hl == 0x3 then
  652.             rB = rB + rA
  653.             return
  654.         elseif hl == 0x4 then
  655.             rI = rI + _band( opcode, 0xFF )
  656.             return
  657.         elseif hl == 0x5 then
  658.             rI = rI + rA
  659.             return
  660.         elseif hl == 0x6 then
  661.             rI = rI + rB
  662.             return
  663.         elseif hl == 0x7 then
  664.             rB = mrAndom( 0, 0xFF )
  665.             return
  666.         elseif hl == 0x8 then
  667.             rA = rA - _band( opcode, 0xFF )
  668.             return
  669.         elseif hl == 0x9 then
  670.             rA = rA - rB
  671.             return
  672.         elseif hl == 0xA then
  673.             rB = rB - _band( opcode, 0xFF )
  674.             return
  675.         elseif hl == 0xB then
  676.             rB = rB - rA
  677.             return
  678.         elseif hl == 0xC then
  679.             rA = rA * _band( opcode, 0xFF )
  680.             return
  681.         elseif hl == 0xD then
  682.             rA = rA * rB
  683.             return
  684.         elseif hl == 0xE then
  685.             rB = rB * _band( opcode, 0xFF )
  686.             return
  687.         elseif hl == 0xF then
  688.             rB = rB * rA
  689.             return
  690.         end
  691.     elseif op == 0x6000 then
  692.         local hl = _band( _rshift( opcode, 8 ), 0xF )
  693.         if hl == 0x0 then
  694.             if (rA == rB) == false then
  695.                 rPC = rPC + 2
  696.             end
  697.             return
  698.         elseif hl == 0x1 then
  699.             if (rA == _band( opcode, 0x00FF )) == false then
  700.                 rPC = rPC + 2
  701.             end
  702.             return
  703.         elseif hl == 0x2 then
  704.             if (rB == _band( opcode, 0xFF )) == false then
  705.                 rPC = rPC + 2
  706.             end
  707.             return
  708.         elseif hl == 0x3 then
  709.             if (rA >= rB) == false then
  710.                 rPC = rPC + 2
  711.             end
  712.             return
  713.         elseif hl == 0x4 then
  714.             if (rA >= _band( opcode, 0xFF )) == false then
  715.                 rPC = rPC + 2
  716.             end
  717.             return
  718.         elseif hl == 0x5 then
  719.             if (rB >= _band( opcode, 0xFF )) == false then
  720.                 rPC = rPC + 2
  721.             end
  722.             return
  723.         elseif hl == 0x6 then
  724.             if (rA <= rB) == false then
  725.                 rPC = rPC + 2
  726.             end
  727.             return
  728.         elseif hl == 0x7 then
  729.             if (rA <= _band( opcode, 0xFF )) == false then
  730.                 rPC = rPC + 2
  731.             end
  732.             return
  733.         elseif hl == 0x8 then
  734.             if (rB <= _band( opcode, 0xFF )) == false then
  735.                 rPC = rPC + 2
  736.             end
  737.             return
  738.         elseif hl == 0x9 then
  739.             if (rA > rB) == false then
  740.                 rPC = rPC + 2
  741.             end
  742.             return
  743.         elseif hl == 0xA then
  744.             if (rA > _band( opcode, 0xFF )) == false then
  745.                 rPC = rPC + 2
  746.             end
  747.             return
  748.         elseif hl == 0xB then
  749.             if (rB > _band( opcode, 0xFF )) == false then
  750.                 rPC = rPC + 2
  751.             end
  752.             return
  753.         elseif hl == 0xC then
  754.             if (rA < rB) == false then
  755.                 rPC = rPC + 2
  756.             end
  757.             return
  758.         elseif hl == 0xD then
  759.             if (rA < _band( opcode, 0xFF )) == false then
  760.                 rPC = rPC + 2
  761.             end
  762.             return
  763.         elseif hl == 0xE then
  764.             if (rB < _band( opcode, 0xFF )) == false then
  765.                 rPC = rPC + 2
  766.             end
  767.             return
  768.         end
  769.     elseif op == 0x7000 then
  770.         local hl = _band( _rshift( opcode, 8 ), 0xF )
  771.         if hl == 0 then
  772.             MEM[ rI ] = rA
  773.             return
  774.         elseif hl == 1 then
  775.             MEM[ rI ] = rB
  776.             return
  777.         elseif hl == 2 then
  778.             MEM[ rI ] = _band( opcode, 0xFF )
  779.             return
  780.         elseif hl == 3 then
  781.             rA = MEM[ rI ]
  782.             return
  783.         elseif hl == 4 then
  784.             rB = MEM[ rI ]
  785.             return
  786.         elseif hl == 5 then
  787.             rX = rI
  788.             return
  789.         elseif hl == 6 then
  790.             rI = rX
  791.             return
  792.         end
  793.     elseif op == 0x8000 then
  794.         rI = _band( opcode, 0xFFF )
  795.         return
  796.     elseif op == 0x9000 then
  797.         rX = _band( opcode, 0xFFF )
  798.         return
  799.     elseif op == 0xA000 then
  800.         local hl = _band( _rshift( opcode, 8 ), 0xF )
  801.         if hl == 0x0 then
  802.             rX = rX + _band( opcode, 0xFF )
  803.             return
  804.         elseif hl == 0x1 then
  805.             rX = rX + rA
  806.             return
  807.         elseif hl == 0x2 then
  808.             rX = rX + rB
  809.             return
  810.         elseif hl == 0x3 then
  811.             rX = rX - _band( opcode, 0xFF )
  812.             return
  813.         elseif hl == 0x4 then
  814.             rX = rX - rA
  815.             return
  816.         elseif hl == 0x5 then
  817.             rX = rX - rB
  818.             return
  819.         elseif hl == 0x6 then
  820.             rX = rX * _band( opcode, 0xFF )
  821.             return
  822.         elseif hl == 0x7 then
  823.             rX = rX * rA
  824.             return
  825.         elseif hl == 0x8 then
  826.             rX = rX * rB
  827.             return
  828.         end
  829.     end
  830. end
  831.  
  832. local function loadROM( fn )
  833.     printError( "LOADING..." )
  834.     local file = fs.open( fn, "rb" )
  835.     local byte = file.read()
  836.     local i = 0
  837.     while byte ~= nil do
  838.         MEM[ i ] = byte
  839.         i = i + 1
  840.         byte = file.read()
  841.     end
  842.     file.close()
  843.     term.setTextColor( colors.green )
  844.     print( "LOADED: '"..fn.."'!" )
  845. end
  846. local toChar = string.char
  847. local mFloor = math.floor
  848. local tSetPos = term.setCursorPos
  849. local tTSetCol = term.setTextColor
  850. local tBSetCol = term.setBackgroundColor
  851. local tWrite = term.write
  852. local osPull = coroutine.yield
  853. local osQueue = os.queueEvent
  854. local BUFFSIZE=(WIDTH*HEIGHT)-1
  855. local function cycle()
  856.     for i=1, 0x25 do
  857.         decode( fetch() )
  858.         if Running == false then break end
  859.         wrapRegs()
  860.         scrBuff[ ((MEM[ 0xFFB ]*WIDTH) + MEM[ 0xFFA ]) % BUFFSIZE ] = {
  861.             toChar( MEM[ 0xFFE ] ),
  862.             COLS[ MEM[ 0xFFD ] ],
  863.             COLS[ MEM[ 0xFFC ] ]
  864.         }
  865.         if MEM[ 0xFFF ] == 1 then
  866.             MEM[ 0xFFF ] = 0
  867.             for i=0, BUFFSIZE do
  868.                 tSetPos( 1+(i % WIDTH), 1+mFloor( i / WIDTH ) )
  869.                 tBSetCol( scrBuff[ i ][ 3 ] )
  870.                 tTSetCol( scrBuff[ i ][ 2 ] )
  871.                 tWrite( scrBuff[ i ][ 1 ] )
  872.             end
  873.         end
  874.     end
  875. end
  876.  
  877. local KEYS = {
  878.     -- P1
  879.     [ keys.up ] = 0x1; -- UP
  880.     [ keys.down ] = 0x2; -- DOWN
  881.     [ keys.left ] = 0x3; -- LEFT
  882.     [ keys.right ] = 0x4; -- RIGHT
  883.     [  keys.a ] = 0x5; -- A
  884.     [  keys.b ] = 0x6; -- B
  885.  
  886.     -- P2
  887.     [  72 ] = 0x7; -- UP
  888.     [  76 ] = 0x8; -- DOWN
  889.     [  75 ] = 0x9; -- LEFT
  890.     [  77 ] = 0xA; -- RIGHT
  891.     [  51 ] = 0xB; -- A
  892.     [  52 ] = 0xC; -- B
  893. }
  894.  
  895. if #tArgs > 1 then
  896.     makeByteFile( compile( parse( tokenize( inputStream( tArgs[ 1 ] ) ) ) ), tArgs[ 2 ] )
  897.     term.setTextColor( colors.green )
  898.     print( "COMPILED!" )
  899.     return
  900. end
  901. reset()
  902. loadROM( tArgs[ 1 ] )
  903. local myTimer = os.startTimer( 0.01 )
  904. while Running do
  905.     local ev, val = osPull()
  906.     if ev == "key" then
  907.         MEM[ 0xFF9 ] = KEYS[ val ] or 0
  908.     elseif ev == "key_up" then
  909.         MEM[ 0xFF9 ] = 0
  910.     elseif ev == "timer" then
  911.         if val == myTimer then
  912.             cycle()
  913.             myTimer = os.startTimer( 0.01 )
  914.         end
  915.     elseif ev == "terminate" then
  916.         break
  917.     end
  918. end
  919. term.setBackgroundColor( bckColLast )
  920. term.setTextColor( txtColLast )
  921. term.setCursorPos( 1, 1 )
  922. term.clear()
  923. print( "Chiper stopped." )
Advertisement
Add Comment
Please, Sign In to add comment