Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <memory>
- #include <vector>
- #include <list>
- #include <map>
- #define EXCEPT_UNKNOWN_TYPE "Component type is unknown"
- #define EXCEPT_UNKNOWN_NAME "Component name is unknown"
- #define EXCEPT_ERR_SYNT_LEX "Ciruit file includes one or serveral lexical/syntactical errors"
- #define EXCEPT_NOEXIST_PIN "Requested pin does not exist"
- #define EXCEPT_DUPLICATE_NAME "Several components share the same name"
- #define EXCEPT_NOTLINKED_OUTPUT "One or several outputs are not linked"
- #define EXCEPT_NOVALUE_INPUT "One or several inputs values aren't provided"
- #define EXCEPT_UNKNOWN_INPUT "A provided input is unknown"
- #define EXCEPT_NOSECTION_CHIPSET "Circuit file doesn't provide a chipset section"
- #define EXCEPT_NOSECTION_LINKS "Circuit file doesn't provide a links section"
- namespace nts
- {
- enum Tristate {
- UNDEFINED = (-true),
- TRUE = true,
- FALSE = false
- };
- class IComponent
- {
- public:
- virtual ~IComponent () = default;
- public:
- virtual nts::Tristate compute(std::size_t pin = 1) = 0;
- virtual void setLink(std::size_t pin, nts::IComponent &other, std::size_t otherPin) = 0;
- virtual void dump() const = 0;
- };
- class Simulator
- {
- /* Displays ">" as a prompt between commands
- * Print undefined values as "U"
- * Run a simulation upon launch and then a display command
- * Must stop the program when CTRL + D is received */
- public:
- Simulator(std::list<std::string> components, std::list<std::string> value, std::list<std::string> links);
- void exit_simulator(); // exit the simulator
- void display_outputs_value(); // display all the outputs value in ascii order
- void change_input_value(); // does not apply to clocks
- void simulate(); // run the simulation once
- void loop_simulate(); // continuously run the simulation w/o displaying a prompt, until SIGINT
- void dump_components(); // call the dump method of every component
- private:
- std::map<std::string, nts::IComponent *> _componentsList;
- };
- class Pins
- {
- public:
- typedef enum {
- IN,
- OUT,
- VSS,
- VDD
- } pTypes;
- public:
- Pins();
- ~Pins();
- void setType(pTypes);
- void setState(Tristate);
- nts::Tristate compute();
- protected:
- nts::IComponent *linked_component;
- size_t target_pin;
- nts::Pins::pTypes _type;
- nts::Tristate _state;
- };
- class AbsComponent : public IComponent
- {
- public:
- AbsComponent(std::string);
- ~AbsComponent();
- virtual nts::Tristate compute(std::size_t pin = 1);
- virtual void setLink(std::size_t pin, nts::IComponent &other, std::size_t otherPin);
- virtual void dump() const;
- std::string getName() const;
- cTypes getType() const;
- std::vector<Pins> getPins();
- void setPinState(size_t pin, nts::Tristate state);
- protected:
- void setName(std::string);
- void setType(cTypes);
- private:
- std::vector<Pins> _pins;
- std::string _name;
- cTypes _type;
- };
- class cInput : AbsComponent
- {
- public:
- cInput(const std::string &);
- ~cInput();
- void setLink(std::size_t pin, nts::IComponent &other, std::size_t otherPin);
- void dump() const;
- nts::Tristate compute(std::size_t pin = 1);
- };
- class cClock : AbsComponent
- {
- public:
- cClock(const std::string &);
- ~cClock();
- void setLink(std::size_t pin, nts::IComponent &other, std::size_t otherPin);
- void dump() const;
- nts::Tristate compute(std::size_t pin = 1);
- };
- class cTrue : AbsComponent
- {
- public:
- cTrue(const std::string &);
- ~cTrue();
- void setLink(std::size_t pin, nts::IComponent &other, std::size_t otherPin);
- void dump() const;
- nts::Tristate compute(std::size_t pin = 1);
- };
- class cFalse : AbsComponent
- {
- public:
- cFalse(const std::string &);
- ~cFalse();
- void setLink(std::size_t pin, nts::IComponent &other, std::size_t otherPin);
- void dump() const;
- nts::Tristate compute(std::size_t pin = 1);
- };
- class cOutput : AbsComponent
- {
- public:
- cOutput(const std::string &);
- ~cOutput();
- void setLink(std::size_t pin, nts::IComponent &other, std::size_t otherPin);
- void dump() const;
- nts::Tristate compute(std::size_t pin = 1);
- };
- class c4001 : AbsComponent
- {
- public:
- c4001();
- ~c4001();
- void setLink(std::size_t pin, nts::IComponent &other, std::size_t otherPin);
- void dump() const;
- nts::Tristate compute(std::size_t pin = 1);
- };
- class c4008 : AbsComponent
- {
- public:
- c4008();
- ~c4008();
- void setLink(std::size_t pin, nts::IComponent &other, std::size_t otherPin);
- void dump() const;
- nts::Tristate compute(std::size_t pin = 1);
- };
- class c4011 : AbsComponent
- {
- public:
- c4011();
- ~c4011();
- void setLink(std::size_t pin, nts::IComponent &other, std::size_t otherPin);
- void dump() const;
- nts::Tristate compute(std::size_t pin = 1);
- };
- class c4013 : AbsComponent
- {
- public:
- c4013();
- ~c4013();
- void setLink(std::size_t pin, nts::IComponent &other, std::size_t otherPin);
- void dump() const;
- nts::Tristate compute(std::size_t pin = 1);
- };
- class c4017 : AbsComponent
- {
- public:
- c4017();
- ~c4017();
- void setLink(std::size_t pin, nts::IComponent &other, std::size_t otherPin);
- void dump() const;
- nts::Tristate compute(std::size_t pin = 1);
- };
- class c4030 : AbsComponent
- {
- public:
- c4030();
- ~c4030();
- void setLink(std::size_t pin, nts::IComponent &other, std::size_t otherPin);
- void dump() const;
- nts::Tristate compute(std::size_t pin = 1);
- };
- class c4040 : AbsComponent
- {
- public:
- c4040();
- ~c4040();
- void setLink(std::size_t pin, nts::IComponent &other, std::size_t otherPin);
- void dump() const;
- nts::Tristate compute(std::size_t pin = 1);
- };
- class c4069 : AbsComponent
- {
- public:
- c4069();
- ~c4069();
- void setLink(std::size_t pin, nts::IComponent &other, std::size_t otherPin);
- void dump() const;
- nts::Tristate compute(std::size_t pin = 1);
- };
- class c4071 : AbsComponent
- {
- public:
- c4071();
- ~c4071();
- void setLink(std::size_t pin, nts::IComponent &other, std::size_t otherPin);
- void dump() const;
- nts::Tristate compute(std::size_t pin = 1);
- };
- class c4081 : AbsComponent
- {
- public:
- c4081();
- ~c4081();
- void setLink(std::size_t pin, nts::IComponent &other, std::size_t otherPin);
- void dump() const;
- nts::Tristate compute(std::size_t pin = 1);
- };
- class c4094 : AbsComponent
- {
- public:
- c4094();
- ~c4094();
- void setLink(std::size_t pin, nts::IComponent &other, std::size_t otherPin);
- void dump() const;
- nts::Tristate compute(std::size_t pin = 1);
- };
- class c4514 : AbsComponent
- {
- public:
- c4514();
- ~c4514();
- void setLink(std::size_t pin, nts::IComponent &other, std::size_t otherPin);
- void dump() const;
- nts::Tristate compute(std::size_t pin = 1);
- };
- class c4801 : AbsComponent
- {
- public:
- c4801();
- ~c4801();
- void setLink(std::size_t pin, nts::IComponent &other, std::size_t otherPin);
- void dump() const;
- nts::Tristate compute(std::size_t pin = 1);
- };
- class c2716 : AbsComponent
- {
- public:
- c2716();
- ~c2716();
- void setLink(std::size_t pin, nts::IComponent &other, std::size_t otherPin);
- void dump() const;
- nts::Tristate compute(std::size_t pin = 1);
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement