Example #1
0
        static void Main(string[] args)
        {
            var options = new SwitchOptions();
            if (!CommandLine.ParseArguments(args, options))
                return;

            try
            {
                            if (String.IsNullOrEmpty(options.outFile))
                            {
                                Console.WriteLine("You must supply an outfile.");
                                return;
                            }

                            var source = new System.IO.StreamReader(options.inFile);
                            var lexed = new LexResult();
                            var r = Lexer.Lex(source, lexed);
                            if (r == 0x00)
                            {
                                var destination = System.IO.File.Open(options.outFile, System.IO.FileMode.Create);
                                var writer = new System.IO.BinaryWriter(destination);
                                r = Assembler.Assemble(lexed, writer);
                                writer.Flush();
                                destination.Flush();
                                writer.Close();
                            }
                            Console.WriteLine("Finished with code " + r);
            }
            /*case "emulate":
                        {
                            var source = System.IO.File.Open(options.inFile, System.IO.FileMode.Open);
                            var emulator = new IN8();
                            source.Read(emulator.MEM, 0, 0xFFFF);

                            // Attach devices
                            var teletypeTerminal = new TT3();
                            emulator.AttachHardware(teletypeTerminal, 0x04);

                            while (emulator.STATE_FLAGS == 0x00) emulator.Step();

                            Console.WriteLine(String.Format("Finished in state {0:X2}", emulator.STATE_FLAGS));
                            Console.WriteLine(String.Format("{0:X2} {1:X2} {2:X2} {3:X2} {4:X2} {5:X2} {6:X2} {7:X2}",
                                emulator.A, emulator.B, emulator.C, emulator.D, emulator.E, emulator.H, emulator.L, emulator.O));
                            Console.WriteLine(String.Format("{0:X4} {1:X4} {2:X8}", emulator.IP, emulator.SP, emulator.CLOCK));
                            break;
                        }
                }
            }*/
            catch (Exception e)
            {
                Console.WriteLine("An error occured.");
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
            }
        }
Example #2
0
        public static byte Lex(System.IO.StreamReader code, LexResult into)
        {
            var token = "";
            byte r;
            while (!code.EndOfStream)
            {
                var c = code.Read();
                if (c == '[')
                {
                    r = AddToken(token, into);
                    token = "";
                    if (r != 0x00) return r;

                    while (!code.EndOfStream && c != ']')
                    {
                        token += (char)c;
                        c = code.Read();
                    }

                    if (c != ']') return 0x08;
                    token += ']';

                    r = AddToken(token, into);
                    token = "";
                    if (r != 0x00) return r;
                }
                else if (c == '.' || c == '\n' || c == '\r')
                {
                    r = AddToken(token, into);
                    if (r != 0x00) return r;
                    token = "";
                }
                else if (c == ';')
                {
                    r = AddToken(token, into);
                    token = "";
                    if (r != 0x00) return r;
                    while (!code.EndOfStream && c != '\n' && c != '\r') c = code.Read();
                }
                else
                    token += (char)c;
            }
            r = AddToken(token, into);
            if (r != 0x00) return r;

            Console.WriteLine(String.Format("Binary size: {0:X2} bytes.", into.ByteCount));

            return 0x00;
        }
Example #3
0
 private static byte AddToken(String token, LexResult into)
 {
     token = token.Trim();
     if (String.IsNullOrEmpty(token)) return 0x00;
     if (token[0] == ':')
     {
         if (into.Labels.ContainsKey(token.Substring(1))) return 0x03;
         else into.Labels.Add(token.Substring(1), into.ByteCount);
     }
     else
     {
         into.Tokens.Add(token);
         if (token[0] == '$') into.ByteCount += 2;
         else if (token[0] == '[') into.ByteCount += (ushort)(token.Length - 2);
         else into.ByteCount += 1;
     }
     return 0x00;
 }
Example #4
0
        public static byte Assemble(LexResult lexed, System.IO.BinaryWriter into)
        {
            var encodingTable = IN8.GetEncodingTable();

            foreach (var token in lexed.Tokens)
            {
                if (token.StartsWith("0X"))
                {
                    byte value = 0;
                    if (!byte.TryParse(token.Substring(2), System.Globalization.NumberStyles.HexNumber, null, out value))
                        return 0x02;
                    into.Write(value);
                }
                else if (token.StartsWith("$"))
                {
                    if (lexed.Labels.ContainsKey(token.Substring(1)))
                    {
                        ushort value = lexed.Labels[token.Substring(1)];
                        into.Write((byte)(value >> 8));
                        into.Write((byte)value); //Avoid endian issues.
                    }
                    else
                        return 0x04;
                }
                else if (token.StartsWith("["))
                {
                    foreach (var c in token.Substring(1, token.Length - 2))
                        into.Write((byte)c);
                }
                else if (encodingTable.ContainsKey(token))
                    into.Write(encodingTable[token]);
                else
                    return 0x05;
            }
            return 0x00;
        }