예제 #1
0
        public int DoInterrupt(Dcpu cpu)
        {
            switch (cpu.A)
            {
            case 0:
                _vram = cpu.B;
                return(1);

            case 1:
                _font = cpu.B;
                return(1);

            case 2:
                _palette = cpu.B;
                return(1);

            case 3:
                //_bordercolor = (byte)(cpu.B & 0xf);
                return(1);

            case 4:
                return(256);

            case 5:
                return(16);

            default:
                return(0);
            }
        }
예제 #2
0
        public int DoInterrupt(Dcpu cpu)
        {
            switch (cpu.A)
            {
            case 0:
                _rate     = cpu.B;
                _lastTick = DateTime.UtcNow;
                return(1);

            case 1:
                cpu.C = (ushort)((DateTime.UtcNow - _lastTick).TotalSeconds * cpu.ClockCyclesPerSecond);
                return(1);

            case 2:
                if (cpu.B == 0)
                {
                    _rate = 0;
                }
                else
                {
                    _message = cpu.B;
                }
                return(1);

            default:
                return(0);
            }
        }
예제 #3
0
 public static void Serialize(Dcpu dcpu, BinaryWriter writer)
 {
     writer.Write(MagicNumber);
     writer.Write(dcpu.A);
     writer.Write(dcpu.B);
     writer.Write(dcpu.C);
     writer.Write(dcpu.X);
     writer.Write(dcpu.Y);
     writer.Write(dcpu.Z);
     writer.Write(dcpu.I);
     writer.Write(dcpu.J);
     writer.Write(dcpu.Pc);
     writer.Write(dcpu.Sp);
     writer.Write(dcpu.Ex);
     writer.Write(dcpu.Ia);
     writer.Write(dcpu.InterruptQueueEnabled);
     writer.Write(dcpu.Interrupts.Count);
     foreach (var interrupt in dcpu.Interrupts)
         writer.Write(interrupt.Message);
     var mem = dcpu.Memory;
     var len = mem.Length;
     writer.Write(len);
     for (var i = 0; i < len; i++)
         writer.Write(mem[i]);
 }
예제 #4
0
        public static void Deserialize(Dcpu dcpu, BinaryReader reader)
        {
            if (reader.ReadInt32() != MagicNumber)
            {
                throw new Exception("Unable to read DCPU file: invalid magic number");
            }
            dcpu.Reset(); // Cilph: Remove if you already do this elsewhere
            dcpu.A  = reader.ReadUInt16();
            dcpu.B  = reader.ReadUInt16();
            dcpu.C  = reader.ReadUInt16();
            dcpu.X  = reader.ReadUInt16();
            dcpu.Y  = reader.ReadUInt16();
            dcpu.Z  = reader.ReadUInt16();
            dcpu.I  = reader.ReadUInt16();
            dcpu.J  = reader.ReadUInt16();
            dcpu.Pc = reader.ReadUInt16();
            dcpu.Sp = reader.ReadUInt16();
            dcpu.Ex = reader.ReadUInt16();
            dcpu.Ia = reader.ReadUInt16();
            dcpu.InterruptQueueEnabled = reader.ReadBoolean();
            var interruptCount = reader.ReadInt32();

            for (var i = 0; i < interruptCount; i++)
            {
                dcpu.Interrupts.Enqueue(new Interrupt(reader.ReadUInt16()));
            }
            var len = reader.ReadInt32();
            var mem = new ushort[len];

            for (var i = 0; i < len; i++)
            {
                mem[i] = reader.ReadUInt16();
            }
        }
예제 #5
0
        public static void Serialize(Dcpu dcpu, BinaryWriter writer)
        {
            writer.Write(MagicNumber);
            writer.Write(dcpu.A);
            writer.Write(dcpu.B);
            writer.Write(dcpu.C);
            writer.Write(dcpu.X);
            writer.Write(dcpu.Y);
            writer.Write(dcpu.Z);
            writer.Write(dcpu.I);
            writer.Write(dcpu.J);
            writer.Write(dcpu.Pc);
            writer.Write(dcpu.Sp);
            writer.Write(dcpu.Ex);
            writer.Write(dcpu.Ia);
            writer.Write(dcpu.InterruptQueueEnabled);
            writer.Write(dcpu.Interrupts.Count);
            foreach (var interrupt in dcpu.Interrupts)
            {
                writer.Write(interrupt.Message);
            }
            var mem = dcpu.Memory;
            var len = mem.Length;

            writer.Write(len);
            for (var i = 0; i < len; i++)
            {
                writer.Write(mem[i]);
            }
        }
예제 #6
0
 public void ExternalCallback(Dcpu cpu)
 {
     if (_interruptMessage == 0 || _doInterrupt == false)
         return;
     _doInterrupt = false;
     cpu.SendInterrupt(new Interrupt(_interruptMessage));
 }
예제 #7
0
 public static void Deserialize(Dcpu dcpu, BinaryReader reader)
 {
     if (reader.ReadInt32() != MagicNumber)
         throw new Exception("Unable to read DCPU file: invalid magic number");
     dcpu.Reset(); // Cilph: Remove if you already do this elsewhere
     dcpu.A = reader.ReadUInt16();
     dcpu.B = reader.ReadUInt16();
     dcpu.C = reader.ReadUInt16();
     dcpu.X = reader.ReadUInt16();
     dcpu.Y = reader.ReadUInt16();
     dcpu.Z = reader.ReadUInt16();
     dcpu.I = reader.ReadUInt16();
     dcpu.J = reader.ReadUInt16();
     dcpu.Pc = reader.ReadUInt16();
     dcpu.Sp = reader.ReadUInt16();
     dcpu.Ex = reader.ReadUInt16();
     dcpu.Ia = reader.ReadUInt16();
     dcpu.InterruptQueueEnabled = reader.ReadBoolean();
     var interruptCount = reader.ReadInt32();
     for (var i = 0; i < interruptCount; i++)
         dcpu.Interrupts.Enqueue(new Interrupt(reader.ReadUInt16()));
     var len = reader.ReadInt32();
     var mem = new ushort[len];
     for (var i = 0; i < len; i++)
         mem[i] = reader.ReadUInt16();
 }
예제 #8
0
 public void ExternalCallback(Dcpu cpu)
 {
     if (_interruptMessage == 0 || _doInterrupt == false)
     {
         return;
     }
     _doInterrupt = false;
     cpu.SendInterrupt(new Interrupt(_interruptMessage));
 }
예제 #9
0
 public void ExternalCallback(Dcpu cpu)
 {
     if (_rate == 0)
         return;
     var nextTickTime = _lastTick + TimeSpan.FromSeconds(_rate / 60.0);
     if (DateTime.UtcNow < nextTickTime)
         return;
     _lastTick = nextTickTime;
     cpu.SendInterrupt(new Interrupt(_message));
 }
예제 #10
0
        public void ExternalCallback(Dcpu cpu)
        {
            if (_rate == 0)
            {
                return;
            }
            var nextTickTime = _lastTick + TimeSpan.FromSeconds(_rate / 60.0);

            if (DateTime.UtcNow < nextTickTime)
            {
                return;
            }
            _lastTick = nextTickTime;
            cpu.SendInterrupt(new Interrupt(_message));
        }
예제 #11
0
 public int DoInterrupt(Dcpu cpu)
 {
     switch (cpu.A)
     {
         case 0:
             Array.Clear(_keyState, 0, _keyState.Length);
             return 3;
         case 1:
             cpu.C = _keyQueue.Count == 0 ? (ushort)0 : _keyQueue.Dequeue();
             return 2;
         case 2:
             cpu.C = cpu.B >= _keyState.Length ? (ushort)0 : _keyState[cpu.B] ? (ushort)1 : (ushort)0;
             return 2;
         case 3:
             _interruptMessage = cpu.B;
             return 1;
         default:
             return 0;
     }
 }
예제 #12
0
 public int DoInterrupt(Dcpu cpu)
 {
     switch (cpu.A)
     {
         case 0:
             _rate = cpu.B;
             _lastTick = DateTime.UtcNow;
             return 1;
         case 1:
             cpu.C = (ushort)((DateTime.UtcNow - _lastTick).TotalSeconds * cpu.ClockCyclesPerSecond);
             return 1;
         case 2:
             if (cpu.B == 0)
                 _rate = 0;
             else
                 _message = cpu.B;
             return 1;
         default:
             return 0;
     }
 }
예제 #13
0
파일: Program.cs 프로젝트: MSylvia/Bifrost
 static void Main(string[] args)
 {
     if (BitConverter.IsLittleEndian == false)
         Console.WriteLine("Note: You have a big-endian computer, things might behave a bit differently. (But report bugs, as always!)");
     var window = new MainWindow();
     var file = args.Length > 0 ? args[0] : ChooseFile(window);
     var bytes = File.ReadAllBytes(file);
     var shorts = Extensions.ConvertToShorts(bytes, true);
     var dcpu = new Dcpu();
     dcpu.LoadProgram(shorts);
     dcpu.AddHardware(new GenericKeyboard(window));
     dcpu.AddHardware(new Lem1802(window));
     bool[] running = { true };
     var thread = new Thread(() =>
                                 {
                                     while (running[0])
                                         dcpu.Run();
                                 });
     thread.Start();
     Application.Run(window);
     running[0] = false;
 }
예제 #14
0
        public void ExternalCallback(Dcpu cpu)
        {
            if (_vram == 0)
            {
                return;
            }
            var nowIsBlink = DateTime.UtcNow.Millisecond < 500;

            for (var i = 0; i < CWidth * CHeight; i++)
            {
                var word      = cpu.Memory[_vram + i];
                var character = word & 0x7f;
                var blinking  = (word & 0x80) != 0;

                var bgcolor = GetColor(cpu, (byte)(word >> 8 & 0xf));
                var fgcolor = GetColor(cpu, (byte)(word >> 12 & 0xf));

                var bg = ((bgcolor & 0xf00) << 20) | ((bgcolor & 0xf00) << 16) | ((bgcolor & 0xf0) << 12) | ((bgcolor & 0xf0) << 8) | ((bgcolor & 0xf) << 4) | ((bgcolor & 0xf) << 0);
                var fg = ((fgcolor & 0xf00) << 20) | ((fgcolor & 0xf00) << 16) | ((fgcolor & 0xf0) << 12) | ((fgcolor & 0xf0) << 8) | ((fgcolor & 0xf) << 4) | ((fgcolor & 0xf) << 0);

                if (blinking && nowIsBlink)
                {
                    fg = bg;
                }

                var fontWordZero = GetFont(cpu, (byte)(character * 2));
                var fontWordOne  = GetFont(cpu, (byte)(character * 2 + 1));

                var cx = i % CWidth;
                var cy = i / CWidth;
                var px = cx * 4;
                var py = cy * 8;
                PrintToRgb(px + 0, py, (byte)(fontWordZero >> 8), fg, bg);
                PrintToRgb(px + 1, py, (byte)fontWordZero, fg, bg);
                PrintToRgb(px + 2, py, (byte)(fontWordOne >> 8), fg, bg);
                PrintToRgb(px + 3, py, (byte)fontWordOne, fg, bg);
            }
            _systemMonitor.SetScreen(_rgb, Width);
        }
예제 #15
0
        public int DoInterrupt(Dcpu cpu)
        {
            switch (cpu.A)
            {
            case 0:
                Array.Clear(_keyState, 0, _keyState.Length);
                return(3);

            case 1:
                cpu.C = _keyQueue.Count == 0 ? (ushort)0 : _keyQueue.Dequeue();
                return(2);

            case 2:
                cpu.C = cpu.B >= _keyState.Length ? (ushort)0 : _keyState[cpu.B] ? (ushort)1 : (ushort)0;
                return(2);

            case 3:
                _interruptMessage = cpu.B;
                return(1);

            default:
                return(0);
            }
        }
예제 #16
0
파일: Lem1802.cs 프로젝트: MSylvia/Bifrost
 public int DoInterrupt(Dcpu cpu)
 {
     switch (cpu.A)
     {
         case 0:
             _vram = cpu.B;
             return 1;
         case 1:
             _font = cpu.B;
             return 1;
         case 2:
             _palette = cpu.B;
             return 1;
         case 3:
             //_bordercolor = (byte)(cpu.B & 0xf);
             return 1;
         case 4:
             return 256;
         case 5:
             return 16;
         default:
             return 0;
     }
 }
예제 #17
0
 private ushort GetColor(Dcpu cpu, byte colorIndex)
 {
     return(_palette == 0 ? DefaultPalette[colorIndex] : cpu.Memory[_palette + colorIndex]);
 }
예제 #18
0
파일: Lem1802.cs 프로젝트: MSylvia/Bifrost
 private ushort GetFont(Dcpu cpu, byte fontIndex)
 {
     return _font == 0 ? DefaultFont[fontIndex] : cpu.Memory[_font + fontIndex];
 }
예제 #19
0
파일: Lem1802.cs 프로젝트: MSylvia/Bifrost
        public void ExternalCallback(Dcpu cpu)
        {
            if (_vram == 0)
                return;
            var nowIsBlink = DateTime.UtcNow.Millisecond < 500;
            for (var i = 0; i < CWidth * CHeight; i++)
            {
                var word = cpu.Memory[_vram + i];
                var character = word & 0x7f;
                var blinking = (word & 0x80) != 0;

                var bgcolor = GetColor(cpu, (byte)(word >> 8 & 0xf));
                var fgcolor = GetColor(cpu, (byte)(word >> 12 & 0xf));

                var bg = ((bgcolor & 0xf00) << 20) | ((bgcolor & 0xf00) << 16) | ((bgcolor & 0xf0) << 12) | ((bgcolor & 0xf0) << 8) | ((bgcolor & 0xf) << 4) | ((bgcolor & 0xf) << 0);
                var fg = ((fgcolor & 0xf00) << 20) | ((fgcolor & 0xf00) << 16) | ((fgcolor & 0xf0) << 12) | ((fgcolor & 0xf0) << 8) | ((fgcolor & 0xf) << 4) | ((fgcolor & 0xf) << 0);

                if (blinking && nowIsBlink)
                    fg = bg;

                var fontWordZero = GetFont(cpu, (byte)(character * 2));
                var fontWordOne = GetFont(cpu, (byte)(character * 2 + 1));

                var cx = i % CWidth;
                var cy = i / CWidth;
                var px = cx * 4;
                var py = cy * 8;
                PrintToRgb(px + 0, py, (byte)(fontWordZero >> 8), fg, bg);
                PrintToRgb(px + 1, py, (byte)fontWordZero, fg, bg);
                PrintToRgb(px + 2, py, (byte)(fontWordOne >> 8), fg, bg);
                PrintToRgb(px + 3, py, (byte)fontWordOne, fg, bg);
            }
            _systemMonitor.SetScreen(_rgb, Width);
        }
예제 #20
0
파일: Lem1802.cs 프로젝트: MSylvia/Bifrost
 private ushort GetColor(Dcpu cpu, byte colorIndex)
 {
     return _palette == 0 ? DefaultPalette[colorIndex] : cpu.Memory[_palette + colorIndex];
 }
예제 #21
0
 private ushort GetFont(Dcpu cpu, byte fontIndex)
 {
     return(_font == 0 ? DefaultFont[fontIndex] : cpu.Memory[_font + fontIndex]);
 }