Advertisement
Guest User

Untitled

a guest
Feb 27th, 2020
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.03 KB | None | 0 0
  1. #include <iostream>
  2. #include <memory>
  3. #include <vector>
  4. #include <list>
  5. #include <map>
  6.  
  7. #define EXCEPT_UNKNOWN_TYPE "Component type is unknown"
  8. #define EXCEPT_UNKNOWN_NAME "Component name is unknown"
  9. #define EXCEPT_ERR_SYNT_LEX "Ciruit file includes one or serveral lexical/syntactical errors"
  10. #define EXCEPT_NOEXIST_PIN "Requested pin does not exist"
  11. #define EXCEPT_DUPLICATE_NAME "Several components share the same name"
  12. #define EXCEPT_NOTLINKED_OUTPUT "One or several outputs are not linked"
  13. #define EXCEPT_NOVALUE_INPUT "One or several inputs values aren't provided"
  14. #define EXCEPT_UNKNOWN_INPUT "A provided input is unknown"
  15. #define EXCEPT_NOSECTION_CHIPSET "Circuit file doesn't provide a chipset section"
  16. #define EXCEPT_NOSECTION_LINKS "Circuit file doesn't provide a links section"
  17.  
  18.  
  19. namespace nts
  20. {
  21. enum Tristate {
  22. UNDEFINED = (-true),
  23. TRUE = true,
  24. FALSE = false
  25. };
  26.  
  27. class IComponent
  28. {
  29. public:
  30. virtual ~IComponent () = default;
  31. public:
  32. virtual nts::Tristate compute(std::size_t pin = 1) = 0;
  33. virtual void setLink(std::size_t pin, nts::IComponent &other, std::size_t otherPin) = 0;
  34. virtual void dump() const = 0;
  35. };
  36.  
  37. class Simulator
  38. {
  39. /* Displays ">" as a prompt between commands
  40. * Print undefined values as "U"
  41. * Run a simulation upon launch and then a display command
  42. * Must stop the program when CTRL + D is received */
  43.  
  44. public:
  45. Simulator(std::list<std::string> components, std::list<std::string> value, std::list<std::string> links);
  46. void exit_simulator(); // exit the simulator
  47. void display_outputs_value(); // display all the outputs value in ascii order
  48. void change_input_value(); // does not apply to clocks
  49. void simulate(); // run the simulation once
  50. void loop_simulate(); // continuously run the simulation w/o displaying a prompt, until SIGINT
  51. void dump_components(); // call the dump method of every component
  52.  
  53. private:
  54. std::map<std::string, nts::IComponent *> _componentsList;
  55. };
  56.  
  57. class Pins
  58. {
  59. public:
  60. typedef enum {
  61. IN,
  62. OUT,
  63. VSS,
  64. VDD
  65. } pTypes;
  66.  
  67. public:
  68. Pins();
  69. ~Pins();
  70. void setType(pTypes);
  71. void setState(Tristate);
  72. nts::Tristate compute();
  73. protected:
  74. nts::IComponent *linked_component;
  75. size_t target_pin;
  76. nts::Pins::pTypes _type;
  77. nts::Tristate _state;
  78. };
  79.  
  80. class AbsComponent : public IComponent
  81. {
  82. public:
  83. AbsComponent(std::string);
  84. ~AbsComponent();
  85. virtual nts::Tristate compute(std::size_t pin = 1);
  86. virtual void setLink(std::size_t pin, nts::IComponent &other, std::size_t otherPin);
  87. virtual void dump() const;
  88. std::string getName() const;
  89. cTypes getType() const;
  90. std::vector<Pins> getPins();
  91. void setPinState(size_t pin, nts::Tristate state);
  92. protected:
  93. void setName(std::string);
  94. void setType(cTypes);
  95.  
  96. private:
  97. std::vector<Pins> _pins;
  98. std::string _name;
  99. cTypes _type;
  100. };
  101.  
  102. class cInput : AbsComponent
  103. {
  104. public:
  105. cInput(const std::string &);
  106. ~cInput();
  107. void setLink(std::size_t pin, nts::IComponent &other, std::size_t otherPin);
  108. void dump() const;
  109. nts::Tristate compute(std::size_t pin = 1);
  110.  
  111.  
  112. };
  113.  
  114. class cClock : AbsComponent
  115. {
  116. public:
  117. cClock(const std::string &);
  118. ~cClock();
  119. void setLink(std::size_t pin, nts::IComponent &other, std::size_t otherPin);
  120. void dump() const;
  121. nts::Tristate compute(std::size_t pin = 1);
  122.  
  123.  
  124. };
  125.  
  126. class cTrue : AbsComponent
  127. {
  128. public:
  129. cTrue(const std::string &);
  130. ~cTrue();
  131. void setLink(std::size_t pin, nts::IComponent &other, std::size_t otherPin);
  132. void dump() const;
  133. nts::Tristate compute(std::size_t pin = 1);
  134.  
  135.  
  136. };
  137.  
  138. class cFalse : AbsComponent
  139. {
  140. public:
  141. cFalse(const std::string &);
  142. ~cFalse();
  143. void setLink(std::size_t pin, nts::IComponent &other, std::size_t otherPin);
  144. void dump() const;
  145. nts::Tristate compute(std::size_t pin = 1);
  146.  
  147.  
  148. };
  149.  
  150. class cOutput : AbsComponent
  151. {
  152. public:
  153. cOutput(const std::string &);
  154. ~cOutput();
  155. void setLink(std::size_t pin, nts::IComponent &other, std::size_t otherPin);
  156. void dump() const;
  157. nts::Tristate compute(std::size_t pin = 1);
  158.  
  159.  
  160. };
  161.  
  162. class c4001 : AbsComponent
  163. {
  164. public:
  165. c4001();
  166. ~c4001();
  167. void setLink(std::size_t pin, nts::IComponent &other, std::size_t otherPin);
  168. void dump() const;
  169. nts::Tristate compute(std::size_t pin = 1);
  170.  
  171.  
  172. };
  173.  
  174. class c4008 : AbsComponent
  175. {
  176. public:
  177. c4008();
  178. ~c4008();
  179. void setLink(std::size_t pin, nts::IComponent &other, std::size_t otherPin);
  180. void dump() const;
  181. nts::Tristate compute(std::size_t pin = 1);
  182.  
  183.  
  184. };
  185.  
  186. class c4011 : AbsComponent
  187. {
  188. public:
  189. c4011();
  190. ~c4011();
  191. void setLink(std::size_t pin, nts::IComponent &other, std::size_t otherPin);
  192. void dump() const;
  193. nts::Tristate compute(std::size_t pin = 1);
  194.  
  195.  
  196. };
  197. class c4013 : AbsComponent
  198. {
  199. public:
  200. c4013();
  201. ~c4013();
  202. void setLink(std::size_t pin, nts::IComponent &other, std::size_t otherPin);
  203. void dump() const;
  204. nts::Tristate compute(std::size_t pin = 1);
  205.  
  206.  
  207. };
  208. class c4017 : AbsComponent
  209. {
  210. public:
  211. c4017();
  212. ~c4017();
  213. void setLink(std::size_t pin, nts::IComponent &other, std::size_t otherPin);
  214. void dump() const;
  215. nts::Tristate compute(std::size_t pin = 1);
  216.  
  217.  
  218. };
  219.  
  220. class c4030 : AbsComponent
  221. {
  222. public:
  223. c4030();
  224. ~c4030();
  225. void setLink(std::size_t pin, nts::IComponent &other, std::size_t otherPin);
  226. void dump() const;
  227. nts::Tristate compute(std::size_t pin = 1);
  228.  
  229.  
  230. };
  231.  
  232. class c4040 : AbsComponent
  233. {
  234. public:
  235. c4040();
  236. ~c4040();
  237. void setLink(std::size_t pin, nts::IComponent &other, std::size_t otherPin);
  238. void dump() const;
  239. nts::Tristate compute(std::size_t pin = 1);
  240.  
  241.  
  242. };
  243.  
  244. class c4069 : AbsComponent
  245. {
  246. public:
  247. c4069();
  248. ~c4069();
  249. void setLink(std::size_t pin, nts::IComponent &other, std::size_t otherPin);
  250. void dump() const;
  251. nts::Tristate compute(std::size_t pin = 1);
  252.  
  253.  
  254. };
  255.  
  256. class c4071 : AbsComponent
  257. {
  258. public:
  259. c4071();
  260. ~c4071();
  261. void setLink(std::size_t pin, nts::IComponent &other, std::size_t otherPin);
  262. void dump() const;
  263. nts::Tristate compute(std::size_t pin = 1);
  264.  
  265.  
  266. };
  267.  
  268. class c4081 : AbsComponent
  269. {
  270. public:
  271. c4081();
  272. ~c4081();
  273. void setLink(std::size_t pin, nts::IComponent &other, std::size_t otherPin);
  274. void dump() const;
  275. nts::Tristate compute(std::size_t pin = 1);
  276.  
  277.  
  278. };
  279.  
  280. class c4094 : AbsComponent
  281. {
  282. public:
  283. c4094();
  284. ~c4094();
  285. void setLink(std::size_t pin, nts::IComponent &other, std::size_t otherPin);
  286. void dump() const;
  287. nts::Tristate compute(std::size_t pin = 1);
  288.  
  289.  
  290. };
  291.  
  292. class c4514 : AbsComponent
  293. {
  294. public:
  295. c4514();
  296. ~c4514();
  297. void setLink(std::size_t pin, nts::IComponent &other, std::size_t otherPin);
  298. void dump() const;
  299. nts::Tristate compute(std::size_t pin = 1);
  300.  
  301.  
  302. };
  303.  
  304. class c4801 : AbsComponent
  305. {
  306. public:
  307. c4801();
  308. ~c4801();
  309. void setLink(std::size_t pin, nts::IComponent &other, std::size_t otherPin);
  310. void dump() const;
  311. nts::Tristate compute(std::size_t pin = 1);
  312.  
  313.  
  314. };
  315.  
  316. class c2716 : AbsComponent
  317. {
  318. public:
  319. c2716();
  320. ~c2716();
  321. void setLink(std::size_t pin, nts::IComponent &other, std::size_t otherPin);
  322. void dump() const;
  323. nts::Tristate compute(std::size_t pin = 1);
  324.  
  325.  
  326. };
  327. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement