Ejemplo n.º 1
0
 public void MakeComputer(ComputerBuilder builder)
 {
     builder.BuildCpu();
     builder.BuildPrimaryMemory();
     builder.BuildSecondaryMemory();
     builder.BuildGpu();
 }
Ejemplo n.º 2
0
    // Use this for initialization
    void Start()
    {
        //팩토리를 이용한 패턴
        ComputerFactory _ComputerFactory = new ComputerFactory();
        MacComputer     mac = new MacComputer();
        LGComputer      lg  = new LGComputer();
        Computer        com = new Computer();

        //Make Mac
        Computer o;

        _ComputerFactory.Make(mac);
        o = _ComputerFactory.Get();
        Debug.Log(o.ToString());

        // Make lgCom
        _ComputerFactory.Make(lg);
        o = _ComputerFactory.Get();
        Debug.Log(o.ToString());

        //Defalut
        _ComputerFactory.Make(com);
        o = _ComputerFactory.Get();
        Debug.Log(o.ToString());


        // 빌더를 이용한 패턴
        // 빌더를 계속 리턴해서 함수를 계속 불러와 도쿠먼트를 굳이 안찾아 넣어도 됨
        Computer com2 = ComputerBuilder.Start().SetCPU("i7").SetRAM("8g").SetVGA("1070GTX").Build();

        Debug.Log(com2.ToString());
    }
Ejemplo n.º 3
0
        public ComputerProduct(ComputerBuilder theBuilder)
        {
            // Create director for the computerBuilder
            ComputerDirector director = new ComputerDirector();

            theProductData = director.Construct(theBuilder);
        }
Ejemplo n.º 4
0
 public void ConstructComputer(ComputerBuilder computerBuilder)
 {
     computerBuilder.BuildMotherboard();
     computerBuilder.BuildProcessor();
     computerBuilder.BuildHardDisk();
     computerBuilder.BuildScreen();
 }
Ejemplo n.º 5
0
 public Computer(ComputerBuilder builder)
 {
     Hdd = builder.Hdd;
     Ram = builder.Ram;
     GraphicsCardEnabled = builder.GraphicsCardEnabled;
     BluetoothEnabled = builder.BluetoothEnabled;
 }
Ejemplo n.º 6
0
        public void ShouldThrowErrorOnInvalidStringArgument()
        {
            IComputerBuilder builder = new ComputerBuilder();

            var exception = Assert.Throws <ArgumentException>(() => builder.SetHardDrive(12.3, "invalid", 15, 22));

            Assert.AreEqual("Type must be either SSD or HDD", exception.Message);
        }
Ejemplo n.º 7
0
        public void ShouldThrowErrorOnInvalidNumberArgument()
        {
            IComputerBuilder builder = new ComputerBuilder();

            var exception = Assert.Throws <ArgumentException>(() => builder.SetHardDrive(-10, "SSD", 15, 22));

            Assert.AreEqual("Capacity must be greater than 0", exception.Message);
        }
Ejemplo n.º 8
0
 public Computer Build(ComputerBuilder builder)
 {
     builder.SetCores();
     builder.SetCpuFrequency();
     builder.SetDriveType();
     builder.SetRam();
     return(builder.GetComuter());
 }
        public Computer Build(ComputerBuilder builder)
        {
            builder.SetCores();
            builder.SetCpuFrequency();
            builder.SetRam();
            builder.SetDriveType();

            return builder.GetComputer();
        }
    public void Execute()
    {
        ComputerBuilder     builder = new ComputerBuilder();
        IntelBuilderFactory factory = new IntelBuilderFactory();

        builder.BuildFastComputer(factory, 100);

        builder.BuildSlowComputer(factory);
    }
Ejemplo n.º 11
0
 public Computer Construct(ComputerBuilder builder)
 {
     builder.BuilderCPU();
     builder.BuilderMemory();
     builder.BuilderDisk();
     builder.BuilderHost();
     builder.BuilderDisplay();
     return(builder.CreateComputer());
 }
Ejemplo n.º 12
0
        public void Can_Change_Segment_Memory_Bank_By_Setting_Special_Memory_Location()
        {
            // Arrange
            var mem = new Memory(enableBankSwitching: true);

            // Add a new bank to segment 1 with blank RAM. Segment 1 starts at 8192 (0x2000).
            // Adding a new bank does not change current bank number (which will still be 0)
            mem.AddMemorySegmentBank(1);

            // Fill some data in to segment 1 in current bank number (0).
            mem[0x2000] = 0x42;
            mem[0x2001] = 0x21;

            // Load machine code into memory that will switch segment 1 bank from 0 to 1
            ushort codeAddress    = 0xc000;
            ushort codeInsAddress = codeAddress;

            // Prepare memory address 0x02 with memory segment bank number to use. Writing to 0x02 will not actually do the change.
            mem[codeInsAddress++] = (byte)OpCodeId.LDA_I;   // LDA (Load Accumulator)
            mem[codeInsAddress++] = 0x01;                   //  |-Value: The memory segment bank number to put in actual memory
            mem[codeInsAddress++] = (byte)OpCodeId.STA_ZP;  // STA (Store Accumulator)
            mem[codeInsAddress++] = 0x02;                   //  |-ZeroPage address $0002
            // Write the segment number to address 0x01, which will trigger the bank number specified in 0x01 to be loaded in to the segment number written to 0x01.
            mem[codeInsAddress++] = (byte)OpCodeId.LDA_I;   // LDA (Load Accumulator)
            mem[codeInsAddress++] = 0x01;                   //  |-Value: The memory segment number to change.
            mem[codeInsAddress++] = (byte)OpCodeId.STA_ZP;  // STA (Store Accumulator)
            mem[codeInsAddress++] = 0x01;                   //  |-ZeroPage address $0001
            mem[codeInsAddress++] = 0x00;                   // BRK (Break/Force Interrupt) - emulator configured to stop execution when reaching this instruction

            // Initialize emulator with CPU, memory, and execution parameters
            var computerBuilder = new ComputerBuilder();

            computerBuilder
            .WithCPU()
            .WithStartAddress(codeAddress)
            .WithMemory(mem)
            .WithInstructionExecutedEventHandler(
                (s, e) => Debug.WriteLine(OutputGen.GetLastInstructionDisassembly(e.CPU, e.Mem)))
            .WithExecOptions(options =>
            {
                options.ExecuteUntilInstruction = OpCodeId.BRK;     // Emulator will stop executing when a BRK instruction is reached.
            });
            var computer = computerBuilder.Build();

            // Act
            computer.Run();

            // Assert
            // Check that segment 1 now has changed bank number to 1.
            Assert.Equal(1, mem.MemorySegments[1].CurrentBankNumber);
            // Check that content of the memory in segment 1 now is blank (bank 1 was blank when we added it)
            Assert.Equal(0x00, mem[0x2000]);
            Assert.Equal(0x00, mem[0x2001]);
        }
Ejemplo n.º 13
0
 public Computer build(ComputerBuilder computerbuilder)
 {
     computerbuilder.build_computer();
     computerbuilder.set_cpu();
     computerbuilder.set_mainboard();
     computerbuilder.set_ram();
     computerbuilder.set_hdd();
     computerbuilder.set_videocard();
     computerbuilder.set_soundcard();
     return(computerbuilder.computer);
 }
Ejemplo n.º 14
0
        static void Main(string[] args)
        {
            //Computer computer = new Computer("256g ssd", "i7", "8g");
            Computer computer = ComputerBuilder.start()
                                .setCpu("i7")
                                .setRam("8g")
                                .setStorage("255g")
                                .build();

            Console.WriteLine(computer.ToString());
            Console.ReadKey();
        }
Ejemplo n.º 15
0
        public void ShouldBuildEmptyComputer()
        {
            IComputerBuilder builder = new ComputerBuilder();
            var computer             = builder.Build();

            Assert.Null(computer.HardDrive);
            Assert.Null(computer.Motherboard);
            Assert.Null(computer.Cpu);
            Assert.Null(computer.Memory);
            Assert.Null(computer.GraphicsCard);
            Assert.Null(computer.Case);
        }
        public void SetComputerNameWithoutServices()
        {
            var builder = new ComputerBuilder();

            builder.SetName("computer");

            var comp = builder.Build();

            var expected = "computer";
            var actual   = comp.Name;

            Assert.Equal(expected, actual);
        }
Ejemplo n.º 17
0
        private Computer SetupEmulator(EmulatorConfig emulatorConfig)
        {
            Debug.WriteLine($"Loading 6502 machine code binary file.");
            Debug.WriteLine($"{emulatorConfig.ProgramBinaryFile}");
            if (!File.Exists(emulatorConfig.ProgramBinaryFile))
            {
                Debug.WriteLine($"File does not exist.");
                throw new Exception($"Cannot find 6502 binary file: {emulatorConfig.ProgramBinaryFile}");
            }

            var enableBankSwitching = emulatorConfig.Memory.MemoryBanks.EnableMemoryBanks;
            var mem = new Memory(enableBankSwitching: enableBankSwitching);

            if (enableBankSwitching)
            {
                // Add additional memory banks for memory segment 1 (0x2000) and up (segment 0 cannot have multiple banks)
                for (byte memorySegmentNumber = 1; memorySegmentNumber < mem.MemorySegments.Count; memorySegmentNumber++)
                {
                    // By default each segment has one bank when Memory is created above.
                    // Thus we add the specified BanksPerSegment-1 new banks to each segment.
                    for (int i = 0; i < emulatorConfig.Memory.MemoryBanks.BanksPerSegment - 1; i++)
                    {
                        // Add additional memory banks for segment. Memory in those will be blank (0x00).
                        mem.AddMemorySegmentBank(memorySegmentNumber);
                    }
                }
            }

            BinaryLoader.Load(
                mem,
                emulatorConfig.ProgramBinaryFile,
                out ushort loadedAtAddress,
                out int fileLength);

            // Initialize emulator with CPU, memory, and execution parameters
            var computerBuilder = new ComputerBuilder();

            computerBuilder
            .WithCPU()
            .WithStartAddress(loadedAtAddress)
            .WithMemory(mem)
            .WithExecOptions(options =>
            {
                // Emulator will stop executing when a BRK instruction is reached.
                options.ExecuteUntilInstruction = emulatorConfig.StopAtBRK?OpCodeId.BRK:null;
            });

            var computer = computerBuilder.Build();

            return(computer);
        }
Ejemplo n.º 18
0
        public void AddCase_ValidCase_ReturnsComputerWithACase()
        {
            // Arrange
            ComputerBuilder computerBuilder = new ComputerBuilder();

            // Act
            Case myCase = new Case(15, 15, 15, 2, 2);

            computerBuilder.AddCase(myCase);
            var builder = computerBuilder.Build();

            // Assert
            Assert.AreEqual(builder.PCCase, myCase);
        }
Ejemplo n.º 19
0
        public static TestContext NewTestContext(ushort startPos = 0x1000, uint memorySize = 1024 *64)
        {
            var builder = new ComputerBuilder()
                          .WithCPU()
                          .WithMemory(memorySize);

            var computer     = builder.Build();
            var computerCopy = computer.Clone();

            return(new TestContext
            {
                Computer = computer,
                OriginalComputer = computerCopy
            });
        }
Ejemplo n.º 20
0
 /// <summary>
 /// Builder uses a complex series of steps 
 /// </summary>
 /// <param name="vehicleBuilder"> The abstract builder class </param>
 public ProductData Construct(ComputerBuilder computerBuilder)
 {
     // The steps to "streamline" the building process
     // Director class is not neccassary
     computerBuilder.BuildCase();
     computerBuilder.BuildMotherboard();
     computerBuilder.BuildProcessor();
     computerBuilder.BuildPowerSupply();
     computerBuilder.BuildDisplay();
     computerBuilder.BuildInput();
     computerBuilder.BuildAdditionalHardware();
     computerBuilder.BuildRam();
     computerBuilder.BuildHardDrive();
     computerBuilder.BuildOperatingSystem();
     return computerBuilder.Computer;
 }
Ejemplo n.º 21
0
        public void ShouldBuildFullComputerWithBuilderChainedMethods()
        {
            var computer = new ComputerBuilder().SetHardDrive(12.3, "SSD", 15, 22)
                           .SetMotherBoard(4, 500, 3, "ATX", 4)
                           .SetCpu(1200, "AMD", "AM4", 500, 6)
                           .SetMemory(2000, 2000, "DDR4", 16)
                           .SetGraphicsCard(2, 2000, 1400, 4)
                           .SetCase(100, 30, 100, 4, 4)
                           .Build();

            Assert.NotNull(computer.HardDrive);
            Assert.NotNull(computer.Motherboard);
            Assert.NotNull(computer.Cpu);
            Assert.NotNull(computer.Memory);
            Assert.NotNull(computer.GraphicsCard);
            Assert.NotNull(computer.Case);
        }
Ejemplo n.º 22
0
        public Mon()
        {
            var mem = new Memory();

            var computerBuilder = new ComputerBuilder();

            computerBuilder
            .WithCPU()
            //.WithStartAddress()
            .WithMemory(mem)
            .WithInstructionExecutedEventHandler(
                (s, e) => Debug.WriteLine(OutputGen.GetLastInstructionDisassembly(e.CPU, e.Mem)));
            // .WithExecOptions(options =>
            // {
            // });
            Computer = computerBuilder.Build();
        }
Ejemplo n.º 23
0
        public void Computer_Can_Be_Reset_And_Restart_At_ResetVector()
        {
            // Arrange
            var builder = new ComputerBuilder()
                          .WithCPU()
                          .WithMemory(1024 * 64);

            var computer = builder.Build();

            // Act
            computer.Reset();

            // Assert
            Assert.Equal(computer.Mem[CPU.ResetVector], computer.CPU.PC);

            // Not sure if the CPU hardware will have SP set to 0xff on power on, or if there is code in the reset vector in ROM that does this.
            // Assert.Equal(0xff, cpu.SP);
        }
        public void BuildComputerWithServices()
        {
            var builder = new ComputerBuilder();

            var comp = builder.SetName("a")
                       .AddService(new Service("1", ServiceState.Running))
                       .AddService(new Service("2", ServiceState.Stopped))
                       .Build();

            var expected = new HashSet <IService>()
            {
                new Service("1", ServiceState.Running),
                new Service("2", ServiceState.Stopped)
            };

            var actual = comp.EnumerateServices().ToHashSet();

            Assert.Equal(expected, actual);
        }
Ejemplo n.º 25
0
        public void ShouldBuildFullComputerUsingDirector()
        {
            ComputerDirector computerDirector = new ComputerDirector();
            IComputerBuilder builder          = new ComputerBuilder();

            builder.SetHardDrive(12.3, "SSD", 15, 22);
            builder.SetMotherBoard(4, 500, 3, "ATX", 4);
            builder.SetCpu(1200, "AMD", "AM4", 500, 6);
            builder.SetMemory(2000, 2000, "DDR4", 16);
            builder.SetGraphicsCard(2, 2000, 1400, 4);
            builder.SetCase(100, 30, 100, 4, 4);
            var computer = computerDirector.Construct(builder);

            Assert.NotNull(computer.HardDrive);
            Assert.NotNull(computer.Motherboard);
            Assert.NotNull(computer.Cpu);
            Assert.NotNull(computer.Memory);
            Assert.NotNull(computer.GraphicsCard);
            Assert.NotNull(computer.Case);
        }
        public async Task <BaseResponse <ComputerDto> > CreateComputer(CreateComputer request)
        {
            var(errors, isValid) = EntityValidator.Validate(request);

            if (!isValid)
            {
                return(new BaseResponse <ComputerDto>((int)CustomHttpStatusCode.UnprocessableEntity, string.Join("", errors)));
            }

            var computer = new ComputerBuilder().CreateComputer(request.Name, request.Price)
                           .WithGraphicsCards(_mapper.Map <IEnumerable <Domain.GraphicsCard> >(request.GraphicsCard))
                           .WithRams(_mapper.Map <IEnumerable <Ram> >(request.Ram))
                           .Build();

            await _computerRepository.Create(computer);

            var computerDto = _mapper.Map <ComputerDto>(computer);

            return(new BaseResponse <ComputerDto>((int)HttpStatusCode.Created, computerDto));
        }
Ejemplo n.º 27
0
        public void BuildProvider()
        {
            var comp1 = new ComputerBuilder()
                        .SetName("1")
                        .AddService(new Service("a", ServicesManager.Services.ServiceState.Running))
                        .Build();

            var comp2 = new ComputerBuilder()
                        .SetName("2")
                        .AddService(new Service("b", ServicesManager.Services.ServiceState.Stopped))
                        .Build();

            var provider = new ProviderBuilder().AddComputer(comp1).AddComputer(comp2).Build();

            var actual   = provider.EnumerateComputers().ToHashSet();
            var expected = new HashSet <IComputer>()
            {
                comp1,
                comp2
            };

            Assert.Equal(expected, actual);
        }
Ejemplo n.º 28
0
        public static void Run()
        {
            Console.WriteLine($"--------------------------------------------------------");
            Console.WriteLine($"Run 6502 code that multiplies two 16 bit signed numbers.");
            Console.WriteLine($"--------------------------------------------------------");

            string prgFileName = "../.cache/ConsoleTestPrograms/AssemblerSource/multiply_2_16bit_numbers.prg";

            Console.WriteLine("");
            Console.WriteLine($"Loading binary into emulator memory...");

            var mem = BinaryLoader.Load(
                prgFileName,
                out ushort loadedAtAddress);

            Console.WriteLine($"Loading done.");
            Console.WriteLine("");
            Console.WriteLine($"Data & code load address:  {loadedAtAddress.ToHex(), 10} ({loadedAtAddress})");
            Console.WriteLine($"Code start address:        {loadedAtAddress.ToHex(), 10} ({loadedAtAddress})");

            ushort valA = 1337;
            ushort valB = 42;

            Console.WriteLine("");
            Console.WriteLine($"Multiply {valA.ToDecimalAndHex()} with {valB.ToDecimalAndHex()}");

            ushort sourceAddressA = 0xd000;
            ushort sourceAddressB = 0xd002;
            ushort resultAddress  = 0xd004;

            mem[(ushort)(sourceAddressA + 0)] = valA.Lowbyte();
            mem[(ushort)(sourceAddressA + 1)] = valA.Highbyte();
            mem[(ushort)(sourceAddressB + 0)] = valB.Lowbyte();
            mem[(ushort)(sourceAddressB + 1)] = valB.Highbyte();
            Console.WriteLine("");
            Console.WriteLine($"Value A set memory location {sourceAddressA.ToHex()}");
            Console.WriteLine($"{((ushort)(sourceAddressA + 0)).ToHex()} : {mem[(ushort)(sourceAddressA + 0)].ToHex()}");
            Console.WriteLine($"{((ushort)(sourceAddressA + 1)).ToHex()} : {mem[(ushort)(sourceAddressA + 1)].ToHex()}");
            Console.WriteLine($"Value B set memory location {sourceAddressB.ToHex()}");
            Console.WriteLine($"{((ushort)(sourceAddressB + 0)).ToHex()} : {mem[(ushort)(sourceAddressB + 0)].ToHex()}");
            Console.WriteLine($"{((ushort)(sourceAddressB + 1)).ToHex()} : {mem[(ushort)(sourceAddressB + 1)].ToHex()}");

            // Initialize CPU, set PC to start position
            var computerBuilder = new ComputerBuilder();

            computerBuilder
            .WithCPU()
            .WithStartAddress(loadedAtAddress)
            .WithMemory(mem)
            .WithInstructionExecutedEventHandler(
                (s, e) => Console.WriteLine($"{e.CPU.PC.ToHex()}: {e.CPU.ExecState.LastOpCode.Value.ToOpCodeId()}"))
            .WithExecOptions(options =>
            {
                options.ExecuteUntilInstruction = OpCodeId.BRK;
            });

            var computer = computerBuilder.Build();

            Console.WriteLine("");
            Console.WriteLine("Running 6502 multiplication routine...");
            computer.Run();

            Console.WriteLine("");
            Console.WriteLine("Done.");
            Console.WriteLine("");
            Console.WriteLine($"Result stored at {resultAddress.ToHex()}:");
            Console.WriteLine($"{((ushort)(resultAddress + 0)).ToHex()} : {mem[(ushort)(resultAddress + 0)].ToHex()}");
            Console.WriteLine($"{((ushort)(resultAddress + 1)).ToHex()} : {mem[(ushort)(resultAddress + 1)].ToHex()}");

            Console.WriteLine("");
            ushort result = mem.FetchWord(resultAddress);

            Console.WriteLine($"Result:");
            Console.WriteLine($"{valA.ToDecimalAndHex()} * {valB.ToDecimalAndHex()} = {result.ToDecimalAndHex()}");
        }
 public void Build(ComputerBuilder computerBuilder)
 {
     computerBuilder.BuildOS();
     computerBuilder.BuildDevice();
 }
Ejemplo n.º 30
0
        public void Run()
        {
            Console.WriteLine($"------------------------------------------------------------------");
            Console.WriteLine($"Run 6502 functional test program with decimal tests disabled");
            Console.WriteLine("Functional test downloaded and compiled from:");
            Console.WriteLine($"------------------------------------------------------------------");

            Console.WriteLine($"Downloading functional test source code from");
            Console.WriteLine("https://github.com/Klaus2m5/6502_65C02_functional_tests");
            Console.WriteLine($"Modifying it to set source code line 'disable_decimal = 0' to 'disable_decimal = 1'");
            Console.WriteLine($"And compiling it to a binary that can be loaded into the 6502 emulator...");

            // Set the download directory to the same directory where current application runs in.
            var currentAssemblyLocation = System.Reflection.Assembly.GetEntryAssembly().Location;
            var downloadDir             = System.IO.Path.GetDirectoryName(currentAssemblyLocation);

            var functionalTestBinary = _functionalTestCompiler.Get6502FunctionalTestBinary(
                disableDecimalTests: true,
                downloadDir: downloadDir
                );

            Console.WriteLine($"Download and compilation complete.");
            Console.WriteLine($"Binary location (as well as .lst file):");
            Console.WriteLine($"{functionalTestBinary}");

            // There is no 2 byte header in the 6502_functional_test.bin file.
            // It's supposed to be loaded to memory at 0x0000, and started at 0x0400
            Console.WriteLine("");
            Console.WriteLine($"Loading binary into emulator memory...");
            ushort loadAddress  = 0x000A;
            ushort startAddress = 0x0400;

            var mem = BinaryLoader.Load(
                functionalTestBinary,
                out ushort loadedAtAddress,
                out int fileLength,
                forceLoadAddress: loadAddress);

            Console.WriteLine($"Loading done.");

            // The rest of the bytes are considered the code
            Console.WriteLine("");
            Console.WriteLine($"Data & code load address:  {loadAddress.ToHex(), 10} ({loadAddress})");
            Console.WriteLine($"Code+data length (bytes):  0x{fileLength, -8:X8} ({fileLength})");
            Console.WriteLine($"Code start address:        {startAddress.ToHex(), 10} ({startAddress})");

            //Console.WriteLine("Press Enter to start");
            //Console.ReadLine();

            // Initialize CPU, set PC to start position
            var computerBuilder = new ComputerBuilder();

            computerBuilder
            .WithCPU()
            .WithStartAddress(0x400)
            .WithMemory(mem)
            //.WithInstructionAboutToBeExecutedEventHandler(OnInstructionToBeExecuted)
            .WithInstructionExecutedEventHandler(OnInstructionExecuted)
            .WithUnknownInstructionEventHandler(OnUnknownOpCodeDetected)
            .WithExecOptions(options =>
            {
                options.ExecuteUntilExecutedInstructionAtPC = 0x336d;
                // A successful run has about 26765880 instructions (the version that was run 2021-02-06, that may change)
                // We increase to almost double, and will exit if not finished then.
                options.MaxNumberOfInstructions           = 50000000;
                options.UnknownInstructionThrowsException = false;
            });

            var computer = computerBuilder.Build();

            Console.WriteLine("");
            Console.WriteLine($"If test logic succeeds, the test program will reach a specific memory location: {computer.DefaultExecOptions.ExecuteUntilExecutedInstructionAtPC.Value.ToHex()}, and the emulator will then stop processing.");
            Console.WriteLine($"If test logic fails, the test program will loop forever at the location the error was found. The emulator will try executing a maximum #instructions {computer.DefaultExecOptions.MaxNumberOfInstructions.Value} before giving up.");
            Console.WriteLine($"If unknown opcode is found, it's logged and ignored, and processing continues on next instruction.");

            // Execute program
            Console.WriteLine("");
            Console.WriteLine("Starting code execution...");

            computer.Run();

            Console.WriteLine("");
            Console.WriteLine("Code execution done.");

            var cpu       = computer.CPU;
            var execState = cpu.ExecState;

            Console.WriteLine("");
            Console.WriteLine($"Last instruction:                  {OutputGen.BuildInstructionString(computer.CPU, computer.Mem, computer.CPU.ExecState.PCBeforeLastOpCodeExecuted.Value)}");
            Console.WriteLine($"CPU state:                         {OutputGen.GetProcessorState(computer.CPU)}");
            Console.WriteLine($"Total # CPU instructions executed: {execState.InstructionsExecutionCount}");
            Console.WriteLine($"Total # CPU cycles consumed:       {execState.CyclesConsumed}");

            Console.WriteLine("");
            // Evaluate success/failure
            if (cpu.PC == computer.DefaultExecOptions.ExecuteUntilExecutedInstructionAtPC.Value)
            {
                Console.WriteLine($"Success!");
                Console.WriteLine($"PC reached expected success memory location: {computer.DefaultExecOptions.ExecuteUntilExecutedInstructionAtPC.Value.ToHex()}");
            }
            else
            {
                Console.WriteLine($"Probably failure");
                Console.WriteLine($"The emulator executer a maximum #instructions {computer.DefaultExecOptions.MaxNumberOfInstructions.Value}, and did not manage to get PC to the configured success location: {computer.DefaultExecOptions.ExecuteUntilExecutedInstructionAtPC.Value.ToHex()}");
                Console.WriteLine($"The functional test program would end in a forever-loop on the same memory location if it fails.");
                Console.WriteLine($"Verify the last PC location against the functional test program's .lst file to find out which logic test failed.");
            }
        }
Ejemplo n.º 31
0
 public Computer Build(ComputerBuilder computer)
 {
     return(computer);
 }
Ejemplo n.º 32
0
        public override void Run()
        {
            var data = (DataModel)Data;

            var cb = new ComputerBuilder();

            var amps = new List <Computer>();

            for (int i = 0; i < 5; i++)
            {
                amps.Add(cb.Build());
            }

            var phasePermutations = data.PhaseSettings.Permutations();

            var maxThrusterSignal = 0;
            var withPS            = new[] { 0 };

            foreach (var phaseSetting in phasePermutations)
            {
                foreach (var amp in amps)
                {
                    amp.State = new ComputerState
                    {
                        Memory = (int[])data.Program.Clone()
                    };
                }

                var prevI     = new[] { 4, 0, 1, 2, 3 };
                var ps        = phaseSetting.ToArray();
                var iteration = 0;
                while (iteration > -1)
                {
                    for (int i = 0; i < ps.Length; i++)
                    {
                        if (iteration == 0)
                        {
                            amps[i].State.Inputs.Add(ps[i]);
                        }

                        amps[i].State.Inputs.Add(amps[prevI[i]].State.Output.LastOrDefault());
                        amps[i].State.Halt = false;
                        amps[i].Run();
                    }

                    if (data.FeedbackMode &&
                        amps.Last().State.AwaitingInput)
                    {
                        iteration++;
                    }
                    else
                    {
                        iteration = -1;
                    }
                }

                var signal = amps[4].State.Output.Last();
                if (signal > maxThrusterSignal)
                {
                    maxThrusterSignal = signal;
                    withPS            = ps;
                }
            }


            Result.Add(ThrusterSignalKey, maxThrusterSignal);
            Result.Add(PhaseSettingKey, withPS);
        }
Ejemplo n.º 33
0
        public static void Run()
        {
            // Test program
            // - adds values from two memory location
            // - divides it by 2 (rotate right one bit position)
            // - stores it in another memory location

            // Load input data into memory
            byte   value1        = 12;
            byte   value2        = 30;
            ushort value1Address = 0xd000;
            ushort value2Address = 0xd001;
            ushort resultAddress = 0xd002;
            var    mem           = new Memory();

            mem[value1Address] = value1;
            mem[value2Address] = value2;

            // Load machine code into memory
            ushort codeAddress    = 0xc000;
            ushort codeInsAddress = codeAddress;

            mem[codeInsAddress++] = 0xad;         // LDA (Load Accumulator)
            mem[codeInsAddress++] = 0x00;         //  |-Lowbyte of $d000
            mem[codeInsAddress++] = 0xd0;         //  |-Highbyte of $d000
            mem[codeInsAddress++] = 0x18;         // CLC (Clear Carry flag)
            mem[codeInsAddress++] = 0x6d;         // ADC (Add with Carry, adds memory to accumulator)
            mem[codeInsAddress++] = 0x01;         //  |-Lowbyte of $d001
            mem[codeInsAddress++] = 0xd0;         //  |-Highbyte of $d001
            mem[codeInsAddress++] = 0x6a;         // ROR (Rotate Right, rotates accumulator right one bit position)
            mem[codeInsAddress++] = 0x8d;         // STA (Store Accumulator, store to accumulator to memory)
            mem[codeInsAddress++] = 0x02;         //  |-Lowbyte of $d002
            mem[codeInsAddress++] = 0xd0;         //  |-Highbyte of $d002
            mem[codeInsAddress++] = 0x00;         // BRK (Break/Force Interrupt) - emulator configured to stop execution when reaching this instruction

            // Initialize emulator with CPU, memory, and execution parameters
            var computerBuilder = new ComputerBuilder();

            computerBuilder
            .WithCPU()
            .WithStartAddress(codeAddress)
            .WithMemory(mem)
            .WithInstructionExecutedEventHandler(
                (s, e) => Console.WriteLine(OutputGen.GetLastInstructionDisassembly(e.CPU, e.Mem)))
            .WithExecOptions(options =>
            {
                options.ExecuteUntilInstruction = OpCodeId.BRK;     // Emulator will stop executing when a BRK instruction is reached.
            });
            var computer = computerBuilder.Build();

            // Run program
            computer.Run();
            Console.WriteLine($"Execution stopped");
            Console.WriteLine($"CPU state: {OutputGen.GetProcessorState(computer.CPU)}");
            Console.WriteLine($"Stats: {computer.CPU.ExecState.InstructionsExecutionCount} instruction(s) processed, and used {computer.CPU.ExecState.CyclesConsumed} cycles.");

            // Print result
            byte result = mem[resultAddress];

            Console.WriteLine($"Result: ({value1} + {value2}) / 2 = {result}");
        }