Exemplo n.º 1
0
        public ComputerState(
            IByteRegisterFactory byteRegisterFactory,
            IRam ram,
            IBus1Factory bus1,
            IArithmeticLogicUnitFactory aluFactory,
            ICaezRegisterFactory caezRegisterFactory,
            IRegisterFactory <bool> bitRegisterFactory,
            IBus <IByte> bus,
            IBus <IByte> ioBus)
        {
            Io  = new IoPinStates();
            Bus = bus;
            Bus.AddByte(input =>
            {
                Io.Bus.UpdateData(new BusMessage <IByte> {
                    Name = nameof(Bus), Data = input
                });
                Io.Bus.UpdateSubs();
            });

            Io.Bus.AddByte(input =>
            {
                Bus.UpdateData(new BusMessage <IByte> {
                    Name = nameof(Io), Data = input
                });
            });

            GeneralPurposeRegisters = Enumerable.Range(0, 4).Select(index =>
            {
                var name     = $"GeneralPurposeRegister_{index}";
                var register = byteRegisterFactory.Create(data =>
                {
                    bus.UpdateData(new BusMessage <IByte> {
                        Data = data, Name = name
                    });
                    bus.UpdateSubs();
                }, name);
                bus.AddRegister(register);
                return(register);
            }).ToList();
            Ir = byteRegisterFactory.Create(data => {}, nameof(Ir));
            bus.AddRegister(Ir);
            Iar = byteRegisterFactory.Create(data =>
            {
                bus.UpdateData(new BusMessage <IByte> {
                    Data = data, Name = nameof(Iar)
                });
                bus.UpdateSubs();
            }, nameof(Iar));
            bus.AddRegister(Iar);

            Acc = byteRegisterFactory.Create(b =>
            {
                // for some reason updating subs here blows up, this is being updated in Computer
                bus.UpdateData(new BusMessage <IByte> {
                    Data = b, Name = nameof(Acc)
                });
            }, nameof(Acc));

            Alu = aluFactory.Create(input =>
            {
                Acc.Input = input;
                Acc.Apply();
            }, caez =>
            {
                Flags.Input = caez;
                Flags.Apply();
            });

            CarryInTmp        = bitRegisterFactory.Create(output => { Alu.CarryIn = output; }, nameof(CarryInTmp));
            CarryInTmp.Enable = true;

            Bus1 = bus1.Create(updateAlu =>
            {
                Alu.InputB = updateAlu;
                Alu.Apply();
            });

            Tmp = byteRegisterFactory.Create(wire =>
            {
                Bus1.Input = wire;
                Bus1.Apply();
            }, nameof(Tmp));
            bus.AddRegister(Tmp);

            Flags = caezRegisterFactory.Create(data =>
            {
                CarryInTmp.Input = data.C;
                CarryInTmp.Apply();
            }, nameof(Flags));
            Flags.Enable = true;

            Ram = ram;
            Bus.AddRegister(ram.MemoryAddressRegister);

            bus.AddByte(inputByte =>
            {
                Alu.InputA = inputByte;
                Alu.Apply();
            });
        }