Guest User

[CC] Chiper v3FINAL

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