Guest User

[CC] Chiper v4

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