├── .gitignore
├── LICENSE
├── Makefile
├── README.md
├── alu.c
├── alu.h
├── arithmetic.c
├── arithmetic.h
├── components.c
├── components.h
├── gates.c
├── gates.h
├── main.c
├── memory.c
├── memory.h
├── ram.c
├── ram.h
├── test.c
├── test.h
└── types.h
/.gitignore:
--------------------------------------------------------------------------------
1 | logic
2 | logic.exe
3 | .vs
4 | .vscode
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | GNU GENERAL PUBLIC LICENSE
2 | Version 3, 29 June 2007
3 |
4 | Copyright (C) 2007 Free Software Foundation, Inc.
5 | Everyone is permitted to copy and distribute verbatim copies
6 | of this license document, but changing it is not allowed.
7 |
8 | Preamble
9 |
10 | The GNU General Public License is a free, copyleft license for
11 | software and other kinds of works.
12 |
13 | The licenses for most software and other practical works are designed
14 | to take away your freedom to share and change the works. By contrast,
15 | the GNU General Public License is intended to guarantee your freedom to
16 | share and change all versions of a program--to make sure it remains free
17 | software for all its users. We, the Free Software Foundation, use the
18 | GNU General Public License for most of our software; it applies also to
19 | any other work released this way by its authors. You can apply it to
20 | your programs, too.
21 |
22 | When we speak of free software, we are referring to freedom, not
23 | price. Our General Public Licenses are designed to make sure that you
24 | have the freedom to distribute copies of free software (and charge for
25 | them if you wish), that you receive source code or can get it if you
26 | want it, that you can change the software or use pieces of it in new
27 | free programs, and that you know you can do these things.
28 |
29 | To protect your rights, we need to prevent others from denying you
30 | these rights or asking you to surrender the rights. Therefore, you have
31 | certain responsibilities if you distribute copies of the software, or if
32 | you modify it: responsibilities to respect the freedom of others.
33 |
34 | For example, if you distribute copies of such a program, whether
35 | gratis or for a fee, you must pass on to the recipients the same
36 | freedoms that you received. You must make sure that they, too, receive
37 | or can get the source code. And you must show them these terms so they
38 | know their rights.
39 |
40 | Developers that use the GNU GPL protect your rights with two steps:
41 | (1) assert copyright on the software, and (2) offer you this License
42 | giving you legal permission to copy, distribute and/or modify it.
43 |
44 | For the developers' and authors' protection, the GPL clearly explains
45 | that there is no warranty for this free software. For both users' and
46 | authors' sake, the GPL requires that modified versions be marked as
47 | changed, so that their problems will not be attributed erroneously to
48 | authors of previous versions.
49 |
50 | Some devices are designed to deny users access to install or run
51 | modified versions of the software inside them, although the manufacturer
52 | can do so. This is fundamentally incompatible with the aim of
53 | protecting users' freedom to change the software. The systematic
54 | pattern of such abuse occurs in the area of products for individuals to
55 | use, which is precisely where it is most unacceptable. Therefore, we
56 | have designed this version of the GPL to prohibit the practice for those
57 | products. If such problems arise substantially in other domains, we
58 | stand ready to extend this provision to those domains in future versions
59 | of the GPL, as needed to protect the freedom of users.
60 |
61 | Finally, every program is threatened constantly by software patents.
62 | States should not allow patents to restrict development and use of
63 | software on general-purpose computers, but in those that do, we wish to
64 | avoid the special danger that patents applied to a free program could
65 | make it effectively proprietary. To prevent this, the GPL assures that
66 | patents cannot be used to render the program non-free.
67 |
68 | The precise terms and conditions for copying, distribution and
69 | modification follow.
70 |
71 | TERMS AND CONDITIONS
72 |
73 | 0. Definitions.
74 |
75 | "This License" refers to version 3 of the GNU General Public License.
76 |
77 | "Copyright" also means copyright-like laws that apply to other kinds of
78 | works, such as semiconductor masks.
79 |
80 | "The Program" refers to any copyrightable work licensed under this
81 | License. Each licensee is addressed as "you". "Licensees" and
82 | "recipients" may be individuals or organizations.
83 |
84 | To "modify" a work means to copy from or adapt all or part of the work
85 | in a fashion requiring copyright permission, other than the making of an
86 | exact copy. The resulting work is called a "modified version" of the
87 | earlier work or a work "based on" the earlier work.
88 |
89 | A "covered work" means either the unmodified Program or a work based
90 | on the Program.
91 |
92 | To "propagate" a work means to do anything with it that, without
93 | permission, would make you directly or secondarily liable for
94 | infringement under applicable copyright law, except executing it on a
95 | computer or modifying a private copy. Propagation includes copying,
96 | distribution (with or without modification), making available to the
97 | public, and in some countries other activities as well.
98 |
99 | To "convey" a work means any kind of propagation that enables other
100 | parties to make or receive copies. Mere interaction with a user through
101 | a computer network, with no transfer of a copy, is not conveying.
102 |
103 | An interactive user interface displays "Appropriate Legal Notices"
104 | to the extent that it includes a convenient and prominently visible
105 | feature that (1) displays an appropriate copyright notice, and (2)
106 | tells the user that there is no warranty for the work (except to the
107 | extent that warranties are provided), that licensees may convey the
108 | work under this License, and how to view a copy of this License. If
109 | the interface presents a list of user commands or options, such as a
110 | menu, a prominent item in the list meets this criterion.
111 |
112 | 1. Source Code.
113 |
114 | The "source code" for a work means the preferred form of the work
115 | for making modifications to it. "Object code" means any non-source
116 | form of a work.
117 |
118 | A "Standard Interface" means an interface that either is an official
119 | standard defined by a recognized standards body, or, in the case of
120 | interfaces specified for a particular programming language, one that
121 | is widely used among developers working in that language.
122 |
123 | The "System Libraries" of an executable work include anything, other
124 | than the work as a whole, that (a) is included in the normal form of
125 | packaging a Major Component, but which is not part of that Major
126 | Component, and (b) serves only to enable use of the work with that
127 | Major Component, or to implement a Standard Interface for which an
128 | implementation is available to the public in source code form. A
129 | "Major Component", in this context, means a major essential component
130 | (kernel, window system, and so on) of the specific operating system
131 | (if any) on which the executable work runs, or a compiler used to
132 | produce the work, or an object code interpreter used to run it.
133 |
134 | The "Corresponding Source" for a work in object code form means all
135 | the source code needed to generate, install, and (for an executable
136 | work) run the object code and to modify the work, including scripts to
137 | control those activities. However, it does not include the work's
138 | System Libraries, or general-purpose tools or generally available free
139 | programs which are used unmodified in performing those activities but
140 | which are not part of the work. For example, Corresponding Source
141 | includes interface definition files associated with source files for
142 | the work, and the source code for shared libraries and dynamically
143 | linked subprograms that the work is specifically designed to require,
144 | such as by intimate data communication or control flow between those
145 | subprograms and other parts of the work.
146 |
147 | The Corresponding Source need not include anything that users
148 | can regenerate automatically from other parts of the Corresponding
149 | Source.
150 |
151 | The Corresponding Source for a work in source code form is that
152 | same work.
153 |
154 | 2. Basic Permissions.
155 |
156 | All rights granted under this License are granted for the term of
157 | copyright on the Program, and are irrevocable provided the stated
158 | conditions are met. This License explicitly affirms your unlimited
159 | permission to run the unmodified Program. The output from running a
160 | covered work is covered by this License only if the output, given its
161 | content, constitutes a covered work. This License acknowledges your
162 | rights of fair use or other equivalent, as provided by copyright law.
163 |
164 | You may make, run and propagate covered works that you do not
165 | convey, without conditions so long as your license otherwise remains
166 | in force. You may convey covered works to others for the sole purpose
167 | of having them make modifications exclusively for you, or provide you
168 | with facilities for running those works, provided that you comply with
169 | the terms of this License in conveying all material for which you do
170 | not control copyright. Those thus making or running the covered works
171 | for you must do so exclusively on your behalf, under your direction
172 | and control, on terms that prohibit them from making any copies of
173 | your copyrighted material outside their relationship with you.
174 |
175 | Conveying under any other circumstances is permitted solely under
176 | the conditions stated below. Sublicensing is not allowed; section 10
177 | makes it unnecessary.
178 |
179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
180 |
181 | No covered work shall be deemed part of an effective technological
182 | measure under any applicable law fulfilling obligations under article
183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or
184 | similar laws prohibiting or restricting circumvention of such
185 | measures.
186 |
187 | When you convey a covered work, you waive any legal power to forbid
188 | circumvention of technological measures to the extent such circumvention
189 | is effected by exercising rights under this License with respect to
190 | the covered work, and you disclaim any intention to limit operation or
191 | modification of the work as a means of enforcing, against the work's
192 | users, your or third parties' legal rights to forbid circumvention of
193 | technological measures.
194 |
195 | 4. Conveying Verbatim Copies.
196 |
197 | You may convey verbatim copies of the Program's source code as you
198 | receive it, in any medium, provided that you conspicuously and
199 | appropriately publish on each copy an appropriate copyright notice;
200 | keep intact all notices stating that this License and any
201 | non-permissive terms added in accord with section 7 apply to the code;
202 | keep intact all notices of the absence of any warranty; and give all
203 | recipients a copy of this License along with the Program.
204 |
205 | You may charge any price or no price for each copy that you convey,
206 | and you may offer support or warranty protection for a fee.
207 |
208 | 5. Conveying Modified Source Versions.
209 |
210 | You may convey a work based on the Program, or the modifications to
211 | produce it from the Program, in the form of source code under the
212 | terms of section 4, provided that you also meet all of these conditions:
213 |
214 | a) The work must carry prominent notices stating that you modified
215 | it, and giving a relevant date.
216 |
217 | b) The work must carry prominent notices stating that it is
218 | released under this License and any conditions added under section
219 | 7. This requirement modifies the requirement in section 4 to
220 | "keep intact all notices".
221 |
222 | c) You must license the entire work, as a whole, under this
223 | License to anyone who comes into possession of a copy. This
224 | License will therefore apply, along with any applicable section 7
225 | additional terms, to the whole of the work, and all its parts,
226 | regardless of how they are packaged. This License gives no
227 | permission to license the work in any other way, but it does not
228 | invalidate such permission if you have separately received it.
229 |
230 | d) If the work has interactive user interfaces, each must display
231 | Appropriate Legal Notices; however, if the Program has interactive
232 | interfaces that do not display Appropriate Legal Notices, your
233 | work need not make them do so.
234 |
235 | A compilation of a covered work with other separate and independent
236 | works, which are not by their nature extensions of the covered work,
237 | and which are not combined with it such as to form a larger program,
238 | in or on a volume of a storage or distribution medium, is called an
239 | "aggregate" if the compilation and its resulting copyright are not
240 | used to limit the access or legal rights of the compilation's users
241 | beyond what the individual works permit. Inclusion of a covered work
242 | in an aggregate does not cause this License to apply to the other
243 | parts of the aggregate.
244 |
245 | 6. Conveying Non-Source Forms.
246 |
247 | You may convey a covered work in object code form under the terms
248 | of sections 4 and 5, provided that you also convey the
249 | machine-readable Corresponding Source under the terms of this License,
250 | in one of these ways:
251 |
252 | a) Convey the object code in, or embodied in, a physical product
253 | (including a physical distribution medium), accompanied by the
254 | Corresponding Source fixed on a durable physical medium
255 | customarily used for software interchange.
256 |
257 | b) Convey the object code in, or embodied in, a physical product
258 | (including a physical distribution medium), accompanied by a
259 | written offer, valid for at least three years and valid for as
260 | long as you offer spare parts or customer support for that product
261 | model, to give anyone who possesses the object code either (1) a
262 | copy of the Corresponding Source for all the software in the
263 | product that is covered by this License, on a durable physical
264 | medium customarily used for software interchange, for a price no
265 | more than your reasonable cost of physically performing this
266 | conveying of source, or (2) access to copy the
267 | Corresponding Source from a network server at no charge.
268 |
269 | c) Convey individual copies of the object code with a copy of the
270 | written offer to provide the Corresponding Source. This
271 | alternative is allowed only occasionally and noncommercially, and
272 | only if you received the object code with such an offer, in accord
273 | with subsection 6b.
274 |
275 | d) Convey the object code by offering access from a designated
276 | place (gratis or for a charge), and offer equivalent access to the
277 | Corresponding Source in the same way through the same place at no
278 | further charge. You need not require recipients to copy the
279 | Corresponding Source along with the object code. If the place to
280 | copy the object code is a network server, the Corresponding Source
281 | may be on a different server (operated by you or a third party)
282 | that supports equivalent copying facilities, provided you maintain
283 | clear directions next to the object code saying where to find the
284 | Corresponding Source. Regardless of what server hosts the
285 | Corresponding Source, you remain obligated to ensure that it is
286 | available for as long as needed to satisfy these requirements.
287 |
288 | e) Convey the object code using peer-to-peer transmission, provided
289 | you inform other peers where the object code and Corresponding
290 | Source of the work are being offered to the general public at no
291 | charge under subsection 6d.
292 |
293 | A separable portion of the object code, whose source code is excluded
294 | from the Corresponding Source as a System Library, need not be
295 | included in conveying the object code work.
296 |
297 | A "User Product" is either (1) a "consumer product", which means any
298 | tangible personal property which is normally used for personal, family,
299 | or household purposes, or (2) anything designed or sold for incorporation
300 | into a dwelling. In determining whether a product is a consumer product,
301 | doubtful cases shall be resolved in favor of coverage. For a particular
302 | product received by a particular user, "normally used" refers to a
303 | typical or common use of that class of product, regardless of the status
304 | of the particular user or of the way in which the particular user
305 | actually uses, or expects or is expected to use, the product. A product
306 | is a consumer product regardless of whether the product has substantial
307 | commercial, industrial or non-consumer uses, unless such uses represent
308 | the only significant mode of use of the product.
309 |
310 | "Installation Information" for a User Product means any methods,
311 | procedures, authorization keys, or other information required to install
312 | and execute modified versions of a covered work in that User Product from
313 | a modified version of its Corresponding Source. The information must
314 | suffice to ensure that the continued functioning of the modified object
315 | code is in no case prevented or interfered with solely because
316 | modification has been made.
317 |
318 | If you convey an object code work under this section in, or with, or
319 | specifically for use in, a User Product, and the conveying occurs as
320 | part of a transaction in which the right of possession and use of the
321 | User Product is transferred to the recipient in perpetuity or for a
322 | fixed term (regardless of how the transaction is characterized), the
323 | Corresponding Source conveyed under this section must be accompanied
324 | by the Installation Information. But this requirement does not apply
325 | if neither you nor any third party retains the ability to install
326 | modified object code on the User Product (for example, the work has
327 | been installed in ROM).
328 |
329 | The requirement to provide Installation Information does not include a
330 | requirement to continue to provide support service, warranty, or updates
331 | for a work that has been modified or installed by the recipient, or for
332 | the User Product in which it has been modified or installed. Access to a
333 | network may be denied when the modification itself materially and
334 | adversely affects the operation of the network or violates the rules and
335 | protocols for communication across the network.
336 |
337 | Corresponding Source conveyed, and Installation Information provided,
338 | in accord with this section must be in a format that is publicly
339 | documented (and with an implementation available to the public in
340 | source code form), and must require no special password or key for
341 | unpacking, reading or copying.
342 |
343 | 7. Additional Terms.
344 |
345 | "Additional permissions" are terms that supplement the terms of this
346 | License by making exceptions from one or more of its conditions.
347 | Additional permissions that are applicable to the entire Program shall
348 | be treated as though they were included in this License, to the extent
349 | that they are valid under applicable law. If additional permissions
350 | apply only to part of the Program, that part may be used separately
351 | under those permissions, but the entire Program remains governed by
352 | this License without regard to the additional permissions.
353 |
354 | When you convey a copy of a covered work, you may at your option
355 | remove any additional permissions from that copy, or from any part of
356 | it. (Additional permissions may be written to require their own
357 | removal in certain cases when you modify the work.) You may place
358 | additional permissions on material, added by you to a covered work,
359 | for which you have or can give appropriate copyright permission.
360 |
361 | Notwithstanding any other provision of this License, for material you
362 | add to a covered work, you may (if authorized by the copyright holders of
363 | that material) supplement the terms of this License with terms:
364 |
365 | a) Disclaiming warranty or limiting liability differently from the
366 | terms of sections 15 and 16 of this License; or
367 |
368 | b) Requiring preservation of specified reasonable legal notices or
369 | author attributions in that material or in the Appropriate Legal
370 | Notices displayed by works containing it; or
371 |
372 | c) Prohibiting misrepresentation of the origin of that material, or
373 | requiring that modified versions of such material be marked in
374 | reasonable ways as different from the original version; or
375 |
376 | d) Limiting the use for publicity purposes of names of licensors or
377 | authors of the material; or
378 |
379 | e) Declining to grant rights under trademark law for use of some
380 | trade names, trademarks, or service marks; or
381 |
382 | f) Requiring indemnification of licensors and authors of that
383 | material by anyone who conveys the material (or modified versions of
384 | it) with contractual assumptions of liability to the recipient, for
385 | any liability that these contractual assumptions directly impose on
386 | those licensors and authors.
387 |
388 | All other non-permissive additional terms are considered "further
389 | restrictions" within the meaning of section 10. If the Program as you
390 | received it, or any part of it, contains a notice stating that it is
391 | governed by this License along with a term that is a further
392 | restriction, you may remove that term. If a license document contains
393 | a further restriction but permits relicensing or conveying under this
394 | License, you may add to a covered work material governed by the terms
395 | of that license document, provided that the further restriction does
396 | not survive such relicensing or conveying.
397 |
398 | If you add terms to a covered work in accord with this section, you
399 | must place, in the relevant source files, a statement of the
400 | additional terms that apply to those files, or a notice indicating
401 | where to find the applicable terms.
402 |
403 | Additional terms, permissive or non-permissive, may be stated in the
404 | form of a separately written license, or stated as exceptions;
405 | the above requirements apply either way.
406 |
407 | 8. Termination.
408 |
409 | You may not propagate or modify a covered work except as expressly
410 | provided under this License. Any attempt otherwise to propagate or
411 | modify it is void, and will automatically terminate your rights under
412 | this License (including any patent licenses granted under the third
413 | paragraph of section 11).
414 |
415 | However, if you cease all violation of this License, then your
416 | license from a particular copyright holder is reinstated (a)
417 | provisionally, unless and until the copyright holder explicitly and
418 | finally terminates your license, and (b) permanently, if the copyright
419 | holder fails to notify you of the violation by some reasonable means
420 | prior to 60 days after the cessation.
421 |
422 | Moreover, your license from a particular copyright holder is
423 | reinstated permanently if the copyright holder notifies you of the
424 | violation by some reasonable means, this is the first time you have
425 | received notice of violation of this License (for any work) from that
426 | copyright holder, and you cure the violation prior to 30 days after
427 | your receipt of the notice.
428 |
429 | Termination of your rights under this section does not terminate the
430 | licenses of parties who have received copies or rights from you under
431 | this License. If your rights have been terminated and not permanently
432 | reinstated, you do not qualify to receive new licenses for the same
433 | material under section 10.
434 |
435 | 9. Acceptance Not Required for Having Copies.
436 |
437 | You are not required to accept this License in order to receive or
438 | run a copy of the Program. Ancillary propagation of a covered work
439 | occurring solely as a consequence of using peer-to-peer transmission
440 | to receive a copy likewise does not require acceptance. However,
441 | nothing other than this License grants you permission to propagate or
442 | modify any covered work. These actions infringe copyright if you do
443 | not accept this License. Therefore, by modifying or propagating a
444 | covered work, you indicate your acceptance of this License to do so.
445 |
446 | 10. Automatic Licensing of Downstream Recipients.
447 |
448 | Each time you convey a covered work, the recipient automatically
449 | receives a license from the original licensors, to run, modify and
450 | propagate that work, subject to this License. You are not responsible
451 | for enforcing compliance by third parties with this License.
452 |
453 | An "entity transaction" is a transaction transferring control of an
454 | organization, or substantially all assets of one, or subdividing an
455 | organization, or merging organizations. If propagation of a covered
456 | work results from an entity transaction, each party to that
457 | transaction who receives a copy of the work also receives whatever
458 | licenses to the work the party's predecessor in interest had or could
459 | give under the previous paragraph, plus a right to possession of the
460 | Corresponding Source of the work from the predecessor in interest, if
461 | the predecessor has it or can get it with reasonable efforts.
462 |
463 | You may not impose any further restrictions on the exercise of the
464 | rights granted or affirmed under this License. For example, you may
465 | not impose a license fee, royalty, or other charge for exercise of
466 | rights granted under this License, and you may not initiate litigation
467 | (including a cross-claim or counterclaim in a lawsuit) alleging that
468 | any patent claim is infringed by making, using, selling, offering for
469 | sale, or importing the Program or any portion of it.
470 |
471 | 11. Patents.
472 |
473 | A "contributor" is a copyright holder who authorizes use under this
474 | License of the Program or a work on which the Program is based. The
475 | work thus licensed is called the contributor's "contributor version".
476 |
477 | A contributor's "essential patent claims" are all patent claims
478 | owned or controlled by the contributor, whether already acquired or
479 | hereafter acquired, that would be infringed by some manner, permitted
480 | by this License, of making, using, or selling its contributor version,
481 | but do not include claims that would be infringed only as a
482 | consequence of further modification of the contributor version. For
483 | purposes of this definition, "control" includes the right to grant
484 | patent sublicenses in a manner consistent with the requirements of
485 | this License.
486 |
487 | Each contributor grants you a non-exclusive, worldwide, royalty-free
488 | patent license under the contributor's essential patent claims, to
489 | make, use, sell, offer for sale, import and otherwise run, modify and
490 | propagate the contents of its contributor version.
491 |
492 | In the following three paragraphs, a "patent license" is any express
493 | agreement or commitment, however denominated, not to enforce a patent
494 | (such as an express permission to practice a patent or covenant not to
495 | sue for patent infringement). To "grant" such a patent license to a
496 | party means to make such an agreement or commitment not to enforce a
497 | patent against the party.
498 |
499 | If you convey a covered work, knowingly relying on a patent license,
500 | and the Corresponding Source of the work is not available for anyone
501 | to copy, free of charge and under the terms of this License, through a
502 | publicly available network server or other readily accessible means,
503 | then you must either (1) cause the Corresponding Source to be so
504 | available, or (2) arrange to deprive yourself of the benefit of the
505 | patent license for this particular work, or (3) arrange, in a manner
506 | consistent with the requirements of this License, to extend the patent
507 | license to downstream recipients. "Knowingly relying" means you have
508 | actual knowledge that, but for the patent license, your conveying the
509 | covered work in a country, or your recipient's use of the covered work
510 | in a country, would infringe one or more identifiable patents in that
511 | country that you have reason to believe are valid.
512 |
513 | If, pursuant to or in connection with a single transaction or
514 | arrangement, you convey, or propagate by procuring conveyance of, a
515 | covered work, and grant a patent license to some of the parties
516 | receiving the covered work authorizing them to use, propagate, modify
517 | or convey a specific copy of the covered work, then the patent license
518 | you grant is automatically extended to all recipients of the covered
519 | work and works based on it.
520 |
521 | A patent license is "discriminatory" if it does not include within
522 | the scope of its coverage, prohibits the exercise of, or is
523 | conditioned on the non-exercise of one or more of the rights that are
524 | specifically granted under this License. You may not convey a covered
525 | work if you are a party to an arrangement with a third party that is
526 | in the business of distributing software, under which you make payment
527 | to the third party based on the extent of your activity of conveying
528 | the work, and under which the third party grants, to any of the
529 | parties who would receive the covered work from you, a discriminatory
530 | patent license (a) in connection with copies of the covered work
531 | conveyed by you (or copies made from those copies), or (b) primarily
532 | for and in connection with specific products or compilations that
533 | contain the covered work, unless you entered into that arrangement,
534 | or that patent license was granted, prior to 28 March 2007.
535 |
536 | Nothing in this License shall be construed as excluding or limiting
537 | any implied license or other defenses to infringement that may
538 | otherwise be available to you under applicable patent law.
539 |
540 | 12. No Surrender of Others' Freedom.
541 |
542 | If conditions are imposed on you (whether by court order, agreement or
543 | otherwise) that contradict the conditions of this License, they do not
544 | excuse you from the conditions of this License. If you cannot convey a
545 | covered work so as to satisfy simultaneously your obligations under this
546 | License and any other pertinent obligations, then as a consequence you may
547 | not convey it at all. For example, if you agree to terms that obligate you
548 | to collect a royalty for further conveying from those to whom you convey
549 | the Program, the only way you could satisfy both those terms and this
550 | License would be to refrain entirely from conveying the Program.
551 |
552 | 13. Use with the GNU Affero General Public License.
553 |
554 | Notwithstanding any other provision of this License, you have
555 | permission to link or combine any covered work with a work licensed
556 | under version 3 of the GNU Affero General Public License into a single
557 | combined work, and to convey the resulting work. The terms of this
558 | License will continue to apply to the part which is the covered work,
559 | but the special requirements of the GNU Affero General Public License,
560 | section 13, concerning interaction through a network will apply to the
561 | combination as such.
562 |
563 | 14. Revised Versions of this License.
564 |
565 | The Free Software Foundation may publish revised and/or new versions of
566 | the GNU General Public License from time to time. Such new versions will
567 | be similar in spirit to the present version, but may differ in detail to
568 | address new problems or concerns.
569 |
570 | Each version is given a distinguishing version number. If the
571 | Program specifies that a certain numbered version of the GNU General
572 | Public License "or any later version" applies to it, you have the
573 | option of following the terms and conditions either of that numbered
574 | version or of any later version published by the Free Software
575 | Foundation. If the Program does not specify a version number of the
576 | GNU General Public License, you may choose any version ever published
577 | by the Free Software Foundation.
578 |
579 | If the Program specifies that a proxy can decide which future
580 | versions of the GNU General Public License can be used, that proxy's
581 | public statement of acceptance of a version permanently authorizes you
582 | to choose that version for the Program.
583 |
584 | Later license versions may give you additional or different
585 | permissions. However, no additional obligations are imposed on any
586 | author or copyright holder as a result of your choosing to follow a
587 | later version.
588 |
589 | 15. Disclaimer of Warranty.
590 |
591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
599 |
600 | 16. Limitation of Liability.
601 |
602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
610 | SUCH DAMAGES.
611 |
612 | 17. Interpretation of Sections 15 and 16.
613 |
614 | If the disclaimer of warranty and limitation of liability provided
615 | above cannot be given local legal effect according to their terms,
616 | reviewing courts shall apply local law that most closely approximates
617 | an absolute waiver of all civil liability in connection with the
618 | Program, unless a warranty or assumption of liability accompanies a
619 | copy of the Program in return for a fee.
620 |
621 | END OF TERMS AND CONDITIONS
622 |
623 | How to Apply These Terms to Your New Programs
624 |
625 | If you develop a new program, and you want it to be of the greatest
626 | possible use to the public, the best way to achieve this is to make it
627 | free software which everyone can redistribute and change under these terms.
628 |
629 | To do so, attach the following notices to the program. It is safest
630 | to attach them to the start of each source file to most effectively
631 | state the exclusion of warranty; and each file should have at least
632 | the "copyright" line and a pointer to where the full notice is found.
633 |
634 |
635 | Copyright (C)
636 |
637 | This program is free software: you can redistribute it and/or modify
638 | it under the terms of the GNU General Public License as published by
639 | the Free Software Foundation, either version 3 of the License, or
640 | (at your option) any later version.
641 |
642 | This program is distributed in the hope that it will be useful,
643 | but WITHOUT ANY WARRANTY; without even the implied warranty of
644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
645 | GNU General Public License for more details.
646 |
647 | You should have received a copy of the GNU General Public License
648 | along with this program. If not, see .
649 |
650 | Also add information on how to contact you by electronic and paper mail.
651 |
652 | If the program does terminal interaction, make it output a short
653 | notice like this when it starts in an interactive mode:
654 |
655 | Copyright (C)
656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
657 | This is free software, and you are welcome to redistribute it
658 | under certain conditions; type `show c' for details.
659 |
660 | The hypothetical commands `show w' and `show c' should show the appropriate
661 | parts of the General Public License. Of course, your program's commands
662 | might be different; for a GUI interface, you would use an "about box".
663 |
664 | You should also get your employer (if you work as a programmer) or school,
665 | if any, to sign a "copyright disclaimer" for the program, if necessary.
666 | For more information on this, and how to apply and follow the GNU GPL, see
667 | .
668 |
669 | The GNU General Public License does not permit incorporating your program
670 | into proprietary programs. If your program is a subroutine library, you
671 | may consider it more useful to permit linking proprietary applications with
672 | the library. If this is what you want to do, use the GNU Lesser General
673 | Public License instead of this License. But first, please read
674 | .
675 |
--------------------------------------------------------------------------------
/Makefile:
--------------------------------------------------------------------------------
1 | logic: main.c gates.c gates.h arithmetic.c arithmetic.h components.c components.h ram.c ram.h memory.c memory.h alu.c alu.h test.c test.h
2 | gcc -o logic main.c gates.c arithmetic.c components.c ram.c memory.c alu.c test.c -g -Wall
3 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # logic
2 | ## A digital logic simulation written in C
3 | This is an attempt to simulate an eight bit computer, starting from the bare logic gates.
4 | It's based off of the NandGame, which in turn is based off of Nand2Tetris.
5 | I've gotten all of the basic components created. The code, while barely commented, should be pretty self explanatory if you know about digital logic and components like multiplexors.
6 | ## Help Wanted: ALU/instruction set design
7 | I want to do something pretty simple but that has enough functionality to actually do stuff (run a simple OS???). Coding the logic gates and components was the easy part. Any help would be greatly appreciated with this as I don't have a good idea what's necessary in an instruction set to make it theoretically Turing Complete or computationally universal or whatever (ignoring stuff like memory limits).
8 | ## Warning: THIS IS A WORK IN PROGRESS. IT DOESN'T DO ANYTHING COOL RIGHT NOW.
9 | It's set to run the test() function from test.c, which in turn runs test functions for each component (most are commented out right now).
10 | ## It's also not working correctly. I'm having some issues with the combined memory (RAM and registers) component.
11 |
12 | ## Extra Warning: I mean tests in the loosest sense. I'm just visually checking for correct output. I know this isn't best practice, but I'm just an ignorant teenager who hasn't had the time/motivation to learn/teach myself about good testing
13 |
14 | To compile just run ```make```. I use gcc so you can change the Makefile if you use clang or something else. Then just run ```./logic```
15 | ## Yet Another Warning: This has only been tested on a Raspberry Pi 3B running the latest 32-bit Rasberry Pi OS. There's nothing that should prevent it from working on other Linux installations as far as I know. As for Mac and Windows, who knows?
16 |
--------------------------------------------------------------------------------
/alu.c:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | #include "types.h"
4 | #include "gates.h"
5 | #include "arithmetic.h"
6 | #include "components.h"
7 | #include "alu.h"
8 |
9 | //
10 | // Unary
11 | //
12 |
13 | Unary* newUnary() {
14 | Unary* unary = (Unary*)malloc(sizeof(Unary));
15 | initUnary(unary);
16 | return unary;
17 | }
18 |
19 | void initUnary(Unary* unary) {
20 | int i;
21 | unary->zero = BIT_0;
22 | unary->negate = BIT_0;
23 | for (i = 0; i < 8; i++) {
24 | unary->data[i] = BIT_0;
25 | initNot(&unary->not[i]);
26 | }
27 | for (i = 0; i < 2; i++) initMux8(&unary->mux8[i]);
28 | doUnary(unary);
29 | }
30 |
31 | void doUnary(Unary* unary) {
32 | int i;
33 | unary->mux8[0].store = unary->zero;
34 | for (i = 0; i < 8; i++) {
35 | unary->mux8[0].data[0][i] = BIT_0;
36 | unary->mux8[0].data[1][i] = unary->data[i];
37 | }
38 | doMux8(&unary->mux8[0]);
39 | unary->mux8[1].store = unary->negate;
40 | for (i = 0; i < 8; i++) {
41 | unary->not[i].in = unary->mux8[0].out[i];
42 | doNot(&unary->not[i]);
43 | unary->mux8[1].data[0][i] = unary->not[i].out;
44 | unary->mux8[1].data[1][i] = unary->mux8[0].out[i];
45 | }
46 | doMux8(&unary->mux8[1]);
47 | for (i = 0; i < 8; i++) unary->out[i] = unary->mux8[1].out[i];
48 | }
49 |
--------------------------------------------------------------------------------
/alu.h:
--------------------------------------------------------------------------------
1 | #ifndef logic_alu_h
2 | #define logic_alu_h
3 |
4 | typedef struct Unary {
5 | Bit zero, negate;
6 | Bit data[8];
7 | Not not[8];
8 | Mux8 mux8[2];
9 | Bit out[8];
10 | } Unary;
11 |
12 | Unary* newUnary();
13 | void initUnary(Unary* unary);
14 | void doUnary(Unary* unary);
15 |
16 | #endif
17 |
--------------------------------------------------------------------------------
/arithmetic.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 |
4 | #include "gates.h"
5 | #include "arithmetic.h"
6 |
7 | //
8 | // HalfAdd
9 | //
10 |
11 | HalfAdd* newHalfAdd() {
12 | HalfAdd* halfAdd = (HalfAdd*)malloc(sizeof(HalfAdd));
13 | initHalfAdd(halfAdd);
14 | return halfAdd;
15 | }
16 |
17 | void initHalfAdd(HalfAdd* halfAdd) {
18 | int i;
19 | for (i = 0; i < 2; i++) halfAdd->in[i] = '0';
20 | for (i = 0; i < 4; i++) initNand(&halfAdd->nand[i]);
21 | initNot(&halfAdd->not);
22 | doHalfAdd(halfAdd);
23 | }
24 |
25 | void doHalfAdd(HalfAdd* halfAdd) {
26 | halfAdd->nand[0].in[0] = halfAdd->in[0];
27 | halfAdd->nand[0].in[1] = halfAdd->in[1];
28 | doNand(&halfAdd->nand[0]);
29 | halfAdd->nand[1].in[0] = halfAdd->in[0];
30 | halfAdd->nand[1].in[1] = halfAdd->nand[0].out;
31 | doNand(&halfAdd->nand[1]);
32 | halfAdd->nand[2].in[0] = halfAdd->in[1];
33 | halfAdd->nand[2].in[1] = halfAdd->nand[0].out;
34 | doNand(&halfAdd->nand[2]);
35 | halfAdd->nand[3].in[0] = halfAdd->nand[1].out;
36 | halfAdd->nand[3].in[1] = halfAdd->nand[2].out;
37 | doNand(&halfAdd->nand[3]);
38 | halfAdd->not.in = halfAdd->nand[0].out;
39 | doNot(&halfAdd->not);
40 | halfAdd->out[0] = halfAdd->not.out;
41 | halfAdd->out[1] = halfAdd->nand[3].out;
42 | }
43 |
44 | //
45 | // Full add
46 | //
47 |
48 | FullAdd* newFullAdd() {
49 | FullAdd* fullAdd = (FullAdd*)malloc(sizeof(FullAdd));
50 | initFullAdd(fullAdd);
51 | return fullAdd;
52 | }
53 |
54 | void initFullAdd(FullAdd* fullAdd) {
55 | int i;
56 | for (i = 0; i < 3; i++) fullAdd->in[i] = '0';
57 | for (i = 0; i < 8; i++) initNand(&fullAdd->nand[i]);
58 | doFullAdd(fullAdd);
59 | }
60 |
61 | void doFullAdd(FullAdd* fullAdd) {
62 | fullAdd->nand[0].in[0] = fullAdd->in[0];
63 | fullAdd->nand[0].in[1] = fullAdd->in[1];
64 | doNand(&fullAdd->nand[0]);
65 | fullAdd->nand[1].in[0] = fullAdd->in[0];
66 | fullAdd->nand[1].in[1] = fullAdd->nand[0].out;
67 | doNand(&fullAdd->nand[1]);
68 | fullAdd->nand[2].in[0] = fullAdd->in[1];
69 | fullAdd->nand[2].in[1] = fullAdd->nand[0].out;
70 | doNand(&fullAdd->nand[2]);
71 | fullAdd->nand[3].in[0] = fullAdd->nand[1].out;
72 | fullAdd->nand[3].in[1] = fullAdd->nand[2].out;
73 | doNand(&fullAdd->nand[3]);
74 | fullAdd->nand[4].in[0] = fullAdd->in[2];
75 | fullAdd->nand[4].in[1] = fullAdd->nand[3].out;
76 | doNand(&fullAdd->nand[4]);
77 | fullAdd->nand[5].in[0] = fullAdd->nand[3].out;
78 | fullAdd->nand[5].in[1] = fullAdd->nand[4].out;
79 | doNand(&fullAdd->nand[5]);
80 | fullAdd->nand[6].in[0] = fullAdd->in[2];
81 | fullAdd->nand[6].in[1] = fullAdd->nand[4].out;
82 | doNand(&fullAdd->nand[6]);
83 | fullAdd->nand[7].in[0] = fullAdd->nand[5].out;
84 | fullAdd->nand[7].in[1] = fullAdd->nand[6].out;
85 | doNand(&fullAdd->nand[7]);
86 | fullAdd->nand[8].in[0] = fullAdd->nand[0].out;
87 | fullAdd->nand[8].in[1] = fullAdd->nand[4].out;
88 | doNand(&fullAdd->nand[8]);
89 | fullAdd->out[0] = fullAdd->nand[8].out;
90 | fullAdd->out[1] = fullAdd->nand[7].out;
91 | }
92 |
93 | //
94 | // Add
95 | //
96 |
97 | Add* newAdd() {
98 | Add* add = (Add*)malloc(sizeof(Add));
99 | initAdd(add);
100 | return add;
101 | }
102 |
103 | void initAdd(Add* add) {
104 | int i, j;
105 | add->carryIn = '0';
106 | for (i = 0; i < 2; i++) {
107 | for (j = 0; j < 8; j++) {
108 | add->in[i][j] = '0';
109 | }
110 | }
111 | for (i = 0; i < 8; i++) initFullAdd(&add->fullAdd[i]);
112 | doAdd(add);
113 | }
114 |
115 | void doAdd(Add* add) {
116 | int i;
117 | add->fullAdd[0].in[2] = add->carryIn;
118 | for (i = 0; i < 8; i++) {
119 | add->fullAdd[i].in[0] = add->in[0][7-i];
120 | add->fullAdd[i].in[1] = add->in[1][7-i];
121 | doFullAdd(&add->fullAdd[i]);
122 | add->out[7-i] = add->fullAdd[i].out[1];
123 | if (i < 7) add->fullAdd[i + 1].in[2] = add->fullAdd[i].out[0];
124 | }
125 | add->carryOut = add->fullAdd[7].out[0];
126 | }
127 |
128 | //
129 | // Increment
130 | //
131 |
132 | Increment* newIncrement() {
133 | Increment* increment = (Increment*)malloc(sizeof(Increment));
134 | initIncrement(increment);
135 | return increment;
136 | }
137 |
138 | void initIncrement(Increment* increment) {
139 | int i;
140 | for (i = 0; i < 8; i++) increment->in[i] = '0';
141 | initAdd(&increment->add);
142 | initNot(&increment->not);
143 | doIncrement(increment);
144 | }
145 |
146 | void doIncrement(Increment* increment) {
147 | int i;
148 | for (i = 0; i < 8; i++) {
149 | increment->add.in[0][i] = increment->in[i];
150 | increment->add.in[1][i] = '0';
151 | }
152 | // increment->not.in = '0'; // Shouldn't need this if it's initialized
153 | doNot(&increment->not);
154 | increment->add.carryIn = increment->not.out;
155 | doAdd(&increment->add);
156 | for (i = 0; i < 8; i++) {
157 | increment->out[i] = increment->add.out[i];
158 | }
159 | }
160 |
161 | //
162 | // Subtract
163 | //
164 |
165 | Subtract* newSubtract() {
166 | Subtract* subtract = (Subtract*)malloc(sizeof(Subtract));
167 | initSubtract(subtract);
168 | return subtract;
169 | }
170 |
171 | void initSubtract(Subtract* subtract) {
172 | int i, j;
173 | for (i = 0; i < 2; i++) {
174 | for (j = 0; j < 8; j++) {
175 | subtract->in[i][j] = '0';
176 | }
177 | }
178 | for (i = 0; i < 9; i++) initNot(&subtract->not[i]);
179 | initAdd(&subtract->add);
180 | doSubtract(subtract);
181 | }
182 |
183 | void doSubtract(Subtract* subtract) {
184 | int i;
185 | for (i = 0; i < 8; i++) {
186 | subtract->not[i].in = subtract->in[1][i];
187 | doNot(&subtract->not[i]);
188 | subtract->add.in[0][i] = subtract->in[0][i];
189 | subtract->add.in[1][i] = subtract->not[i].out;
190 | }
191 | // subtract->not[8].in = '0'; // not needed if initialized
192 | doNot(&subtract->not[8]);
193 | subtract->add.carryIn = subtract->not[8].out;
194 | doAdd(&subtract->add);
195 | for (i = 0; i < 8; i++) {
196 | subtract->out[i] = subtract->add.out[i];
197 | }
198 | }
199 |
200 | //
201 | // EqualsZero
202 | //
203 |
204 | EqualsZero* newEqualsZero() {
205 | EqualsZero* equalsZero = (EqualsZero*)malloc(sizeof(EqualsZero));
206 | initEqualsZero(equalsZero);
207 | return equalsZero;
208 | }
209 |
210 | void initEqualsZero(EqualsZero* equalsZero) {
211 | int i;
212 | for (i = 0; i < 8; i++) equalsZero->in[i] = '0';
213 | for (i = 0; i < 7; i++) initOr(&equalsZero->or[i]);
214 | initNot(&equalsZero->not);
215 | doEqualsZero(equalsZero);
216 | }
217 |
218 | void doEqualsZero(EqualsZero* equalsZero) {
219 | equalsZero->or[0].in[0] = equalsZero->in[0];
220 | equalsZero->or[0].in[1] = equalsZero->in[1];
221 | doOr(&equalsZero->or[0]);
222 | equalsZero->or[1].in[0] = equalsZero->in[2];
223 | equalsZero->or[1].in[1] = equalsZero->or[0].out;
224 | doOr(&equalsZero->or[1]);
225 | equalsZero->or[2].in[0] = equalsZero->in[3];
226 | equalsZero->or[2].in[1] = equalsZero->or[1].out;
227 | doOr(&equalsZero->or[2]);
228 | equalsZero->or[3].in[0] = equalsZero->in[4];
229 | equalsZero->or[3].in[1] = equalsZero->or[2].out;
230 | doOr(&equalsZero->or[3]);
231 | equalsZero->or[4].in[0] = equalsZero->in[5];
232 | equalsZero->or[4].in[1] = equalsZero->or[3].out;
233 | doOr(&equalsZero->or[4]);
234 | equalsZero->or[5].in[0] = equalsZero->in[6];
235 | equalsZero->or[5].in[1] = equalsZero->or[4].out;
236 | doOr(&equalsZero->or[5]);
237 | equalsZero->or[6].in[0] = equalsZero->in[7];
238 | equalsZero->or[6].in[1] = equalsZero->or[5].out;
239 | doOr(&equalsZero->or[6]);
240 | equalsZero->not.in = equalsZero->or[6].out;
241 | doNot(&equalsZero->not);
242 | equalsZero->out = equalsZero->not.out;
243 | }
244 |
245 | //
246 | // LessThanZero
247 | //
248 |
249 | LessThanZero* newLessThanZero() {
250 | LessThanZero* lessThanZero = (LessThanZero*)malloc(sizeof(LessThanZero));
251 | initLessThanZero(lessThanZero);
252 | return lessThanZero;
253 | }
254 |
255 | void initLessThanZero(LessThanZero* lessThanZero) {
256 | int i;
257 | for (i = 0; i < 8; i++) lessThanZero->in[i] = '0';
258 | doLessThanZero(lessThanZero);
259 | }
260 |
261 | void doLessThanZero(LessThanZero* lessThanZero) {
262 | lessThanZero->out = lessThanZero->in[0];
263 | }
264 |
--------------------------------------------------------------------------------
/arithmetic.h:
--------------------------------------------------------------------------------
1 | #ifndef logic_arithmetic_h
2 | #define logic_arithmetic_h
3 |
4 | typedef struct HalfAdd {
5 | Bit in[2];
6 | Nand nand[4];
7 | Not not;
8 | Bit out[2];
9 | } HalfAdd;
10 |
11 | HalfAdd *newHalfAdd();
12 | void initHalfAdd(HalfAdd *halfAdd);
13 | void doHalfAdd(HalfAdd *halfAdd);
14 |
15 | typedef struct FullAdd {
16 | Bit in[3];
17 | Nand nand[8];
18 | Bit out[2];
19 | } FullAdd;
20 |
21 | FullAdd *newFullAdd();
22 | void initFullAdd(FullAdd *fullAdd);
23 | void doFullAdd(FullAdd *fullAdd);
24 |
25 | typedef struct Add {
26 | Bit in[2][8];
27 | Bit carryIn;
28 | FullAdd fullAdd[8];
29 | Bit out[8];
30 | Bit carryOut;
31 | } Add;
32 |
33 | Add *newAdd();
34 | void initAdd(Add *add);
35 | void doAdd(Add *add);
36 |
37 | typedef struct Increment {
38 | Bit in[8];
39 | Add add;
40 | Not not;
41 | Bit out[8];
42 | } Increment;
43 |
44 | Increment *newIncrement();
45 | void initIncrement(Increment *increment);
46 | void doIncrement(Increment *increment);
47 |
48 | typedef struct Subtract {
49 | Bit in[2][8];
50 | Not not[9];
51 | Add add;
52 | Bit out[8];
53 | } Subtract;
54 |
55 | Subtract *newSubtract();
56 | void initSubtract(Subtract *subtract);
57 | void doSubtract(Subtract *subtract);
58 |
59 | typedef struct EqualsZero {
60 | Bit in[8];
61 | Or or[7];
62 | Not not;
63 | Bit out;
64 | } EqualsZero;
65 |
66 | EqualsZero *newEqualsZero();
67 | void initEqualsZero(EqualsZero *equalsZero);
68 | void doEqualsZero(EqualsZero *equalsZero);
69 |
70 | typedef struct LessThanZero {
71 | Bit in[8];
72 | Bit out;
73 | } LessThanZero;
74 |
75 | LessThanZero* newLessThanZero();
76 | void initLessThanZero(LessThanZero* lessThanZero);
77 | void doLessThanZero(LessThanZero* lessThanZero);
78 |
79 | #endif
80 |
--------------------------------------------------------------------------------
/components.c:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | #include "types.h"
4 | #include "gates.h"
5 | #include "arithmetic.h"
6 | #include "components.h"
7 |
8 | //
9 | // Mux
10 | //
11 |
12 | Mux* newMux() {
13 | Mux* mux = (Mux*)malloc(sizeof(Mux));
14 | initMux(mux);
15 | return mux;
16 | }
17 |
18 | void initMux(Mux* mux) {
19 | int i;
20 | mux->store = BIT_0;
21 | for (i = 0; i < 2; i++) mux->data[i] = BIT_0;
22 | initNot(&mux->not);
23 | for (i = 0; i < 3; i++) initNand(&mux->nand[i]);
24 | doMux(mux);
25 | }
26 |
27 | void doMux(Mux* mux) {
28 | mux->nand[0].in[0] = mux->store;
29 | mux->nand[0].in[1] = mux->data[0];
30 | doNand(&mux->nand[0]);
31 | mux->not.in = mux->store;
32 | doNot(&mux->not);
33 | mux->nand[1].in[0] = mux->not.out;
34 | mux->nand[1].in[1] = mux->data[1];
35 | doNand(&mux->nand[1]);
36 | mux->nand[2].in[0] = mux->nand[0].out;
37 | mux->nand[2].in[1] = mux->nand[1].out;
38 | doNand(&mux->nand[2]);
39 | mux->out = mux->nand[2].out;
40 | }
41 |
42 | //
43 | // Mux8
44 | //
45 |
46 | Mux8* newMux8() {
47 | Mux8* mux8 = (Mux8*)malloc(sizeof(Mux8));
48 | initMux8(mux8);
49 | return mux8;
50 | }
51 |
52 | void initMux8(Mux8* mux8) {
53 | int i, j;
54 | mux8->store = BIT_0;
55 | for (i = 0; i < 2; i++) {
56 | for (j = 0; j < 8; j++) {
57 | mux8->data[i][j] = BIT_0;
58 | }
59 | }
60 | for (i = 0; i < 8; i++) initMux(&mux8->mux[i]);
61 | doMux8(mux8);
62 | }
63 |
64 | void doMux8(Mux8* mux8) {
65 | int i, j;
66 | for (i = 0; i < 8; i++) {
67 | mux8->mux[i].store = mux8->store;
68 | for (j = 0; j < 2; j++) mux8->mux[i].data[j] = mux8->data[j][i];
69 | doMux(&mux8->mux[i]);
70 | mux8->out[i] = mux8->mux[i].out;
71 | }
72 | }
73 |
74 | //
75 | // DMux
76 | //
77 |
78 | DMux* newDMux() {
79 | DMux* dmux = (DMux*)malloc(sizeof(DMux));
80 | initDMux(dmux);
81 | return dmux;
82 | }
83 |
84 | void initDMux(DMux* dmux) {
85 | int i;
86 | dmux->store = BIT_0;
87 | dmux->data = BIT_0;
88 | for (i = 0; i < 2; i++) initAnd(&dmux->and[i]);
89 | initNot(&dmux->not);
90 | doDMux(dmux);
91 | }
92 |
93 | void doDMux(DMux* dmux) {
94 | dmux->and[0].in[0] = dmux->store;
95 | dmux->and[0].in[1] = dmux->data;
96 | doAnd(&dmux->and[0]);
97 | dmux->not.in = dmux->store;
98 | doNot(&dmux->not);
99 | dmux->and[1].in[0] = dmux->not.out;
100 | dmux->and[1].in[1] = dmux->data;
101 | doAnd(&dmux->and[1]);
102 | dmux->out[0] = dmux->and[0].out;
103 | dmux->out[1] = dmux->and[1].out;
104 | }
105 |
106 | //
107 | // Latch
108 | //
109 |
110 | Latch* newLatch() {
111 | Latch* latch = (Latch*)malloc(sizeof(Latch));
112 | initLatch(latch);
113 | return latch;
114 | }
115 |
116 | void initLatch(Latch* latch) {
117 | latch->store = BIT_0;
118 | latch->data = BIT_0;
119 | initMux(&latch->mux);
120 | doLatch(latch);
121 | }
122 |
123 | void doLatch(Latch* latch) {
124 | latch->mux.store = latch->store;
125 | latch->mux.data[0] = latch->data;
126 | latch->mux.data[1] = latch->mux.out;
127 | doMux(&latch->mux);
128 | latch->out = latch->mux.out;
129 | }
130 |
131 | //
132 | // DFF
133 | //
134 |
135 | DFF* newDFF() {
136 | DFF* dff = (DFF*)malloc(sizeof(DFF));
137 | initDFF(dff);
138 | return dff;
139 | }
140 |
141 | void initDFF(DFF* dff) {
142 | int i;
143 | dff->store = BIT_0;
144 | dff->data = BIT_0;
145 | dff->clock = BIT_0;
146 | for (i = 0; i < 2; i++) {
147 | initNand(&dff->nand[i]);
148 | initNot(&dff->not[i]);
149 | initLatch(&dff->latch[i]);
150 | }
151 | doDFF(dff);
152 | }
153 |
154 | void doDFF(DFF* dff) {
155 | dff->nand[0].in[0] = dff->store;
156 | dff->nand[0].in[1] = dff->clock;
157 | doNand(&dff->nand[0]);
158 | dff->nand[1].in[0] = dff->store;
159 | dff->nand[1].in[1] = dff->nand[0].out;
160 | doNand(&dff->nand[1]);
161 | dff->not[0].in = dff->nand[0].out;
162 | doNot(&dff->not[0]);
163 | dff->not[1].in = dff->nand[1].out;
164 | doNot(&dff->not[1]);
165 | dff->latch[0].store = dff->not[1].out;
166 | dff->latch[0].data = dff->data;
167 | doLatch(&dff->latch[0]);
168 | dff->latch[1].store = dff->not[0].out;
169 | dff->latch[1].data = dff->latch[0].out;
170 | doLatch(&dff->latch[1]);
171 | dff->out = dff->latch[1].out;
172 | }
173 |
174 | //
175 | // Register
176 | //
177 |
178 | Register* newRegister() {
179 | Register* reg = (Register*)malloc(sizeof(Register));
180 | initRegister(reg);
181 | return reg;
182 | }
183 |
184 | void initRegister(Register* reg) {
185 | int i;
186 | reg->store = BIT_0;
187 | reg->clock = BIT_0;
188 | for (i = 0; i < 8; i++) {
189 | reg->data[i] = BIT_0;
190 | initDFF(®->dff[i]);
191 | }
192 | doRegister(reg);
193 | }
194 |
195 | void doRegister(Register* reg) {
196 | int i;
197 | for (i = 0; i < 8; i++) {
198 | reg->dff[i].store = reg->store;
199 | reg->dff[i].data = reg->data[i];
200 | reg->dff[i].clock = reg->clock;
201 | doDFF(®->dff[i]);
202 | reg->out[i] = reg->dff[i].out;
203 | }
204 | }
205 |
206 | //
207 | // Counter
208 | //
209 |
210 | Counter* newCounter() {
211 | Counter* counter = (Counter*)malloc(sizeof(Counter));
212 | initCounter(counter);
213 | return counter;
214 | }
215 |
216 | void initCounter(Counter* counter) {
217 | int i;
218 | counter->store = BIT_0;
219 | counter->clock = BIT_0;
220 | for (i = 0; i < 8; i++) counter->data[i] = BIT_0;
221 | initMux8(&counter->mux8);
222 | initNot(&counter->not);
223 | initIncrement(&counter->increment);
224 | initRegister(&counter->reg);
225 | counter->reg.store = counter->not.out;
226 | doCounter(counter);
227 | }
228 |
229 | void doCounter(Counter* counter) {
230 | int i;
231 | counter->mux8.store = counter->store;
232 | counter->reg.clock = counter->clock;
233 |
234 | for (i = 0; i < 8; i++) {
235 | counter->mux8.data[0][i] = counter->data[i];
236 | counter->increment.in[i] = counter->reg.out[i];
237 | }
238 | doIncrement(&counter->increment);
239 |
240 | for (i = 0; i < 8; i++) counter->mux8.data[1][i] = counter->increment.out[i];
241 | doMux8(&counter->mux8);
242 | for (i = 0; i < 8; i++) counter->reg.data[i] = counter->mux8.out[i];
243 | doRegister(&counter->reg);
244 | for (i = 0; i < 8; i++) counter->out[i] = counter->reg.out[i];
245 | }
246 |
--------------------------------------------------------------------------------
/components.h:
--------------------------------------------------------------------------------
1 | #ifndef logic_components_h
2 | #define logic_components_h
3 |
4 | #include "types.h"
5 |
6 | typedef struct Mux {
7 | Bit store;
8 | Bit data[2];
9 | Nand nand[3];
10 | Not not;
11 | Bit out;
12 | } Mux;
13 |
14 | Mux *newMux();
15 | void initMux(Mux *mux);
16 | void doMux(Mux *mux);
17 |
18 | typedef struct Mux8 {
19 | Bit store;
20 | Bit data[2][8];
21 | Mux mux[8];
22 | Bit out[8];
23 | } Mux8;
24 |
25 | Mux8 *newMux8();
26 | void initMux8(Mux8 *mux8);
27 | void doMux8(Mux8 *mux8);
28 |
29 | typedef struct DMux {
30 | Bit store, data;
31 | And and[2];
32 | Not not;
33 | Bit out[2];
34 | } DMux;
35 |
36 | DMux *newDMux();
37 | void initDMux(DMux *dmux);
38 | void doDMux(DMux *dmux);
39 |
40 | typedef struct Latch {
41 | Bit store, data;
42 | Mux mux;
43 | Bit out;
44 | } Latch;
45 |
46 | Latch *newLatch();
47 | void initLatch(Latch *latch);
48 | void doLatch(Latch *latch);
49 |
50 | typedef struct DFF {
51 | Bit store, data, clock;
52 | Nand nand[2];
53 | Not not[2];
54 | Latch latch[2];
55 | Bit out;
56 | } DFF;
57 |
58 | DFF *newDFF();
59 | void initDFF(DFF *dff);
60 | void doDFF(DFF *dff);
61 |
62 | typedef struct Register {
63 | Bit store, clock;
64 | Bit data[8];
65 | DFF dff[8];
66 | Bit out[8];
67 | } Register;
68 |
69 | Register *newRegister();
70 | void initRegister(Register *reg);
71 | void doRegister(Register *reg);
72 |
73 | typedef struct Counter {
74 | Bit store, clock;
75 | Bit data[8];
76 | Mux8 mux8;
77 | Not not;
78 | Increment increment;
79 | Register reg;
80 | Bit out[8];
81 | } Counter;
82 |
83 | Counter *newCounter();
84 | void initCounter(Counter *counter);
85 | void doCounter(Counter *counter);
86 |
87 | // typedef struct RAM {
88 | // Bit store, clock;
89 | // Bit address[8], data[8];
90 |
91 | // Register reg[256];
92 | // }
93 |
94 | #endif
95 |
--------------------------------------------------------------------------------
/gates.c:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | #include "types.h"
4 | #include "gates.h"
5 |
6 | // Each logic gate and circuit is a struct, defined in a header file
7 | // There's an initialization function for each gate/circuit that sets its inputs to zero and does the same to any subcomponents it might have
8 | // The logic gates are all pretty self-explanatory
9 |
10 | //
11 | // Nand
12 | //
13 |
14 | Nand* newNand() {
15 | Nand* nand = (Nand*)malloc(sizeof(Nand));
16 | initNand(nand);
17 | return nand;
18 | }
19 |
20 | void initNand(Nand* nand) {
21 | for (int i = 0; i < 2; i++) nand->in[i] = BIT_0;
22 | doNand(nand);
23 | }
24 |
25 | void doNand(Nand* nand) {
26 | if ((nand->in[0] == BIT_1) && (nand->in[1] == BIT_1)) nand->out = BIT_0;
27 | else nand->out = BIT_1;
28 | }
29 |
30 | //
31 | // Not
32 | //
33 |
34 | Not* newNot() {
35 | Not* not = (Not*)malloc(sizeof(Not));
36 | initNot(not);
37 | return not;
38 | }
39 |
40 | void initNot(Not* not) {
41 | not->in = BIT_0;
42 | initNand(¬->nand);
43 | doNot(not);
44 | }
45 |
46 | void doNot(Not* not) {
47 | not->nand.in[0] = not->in;
48 | not->nand.in[1] = not->in;
49 | doNand(¬->nand);
50 | not->out = not->nand.out;
51 | }
52 |
53 | //
54 | // And
55 | //
56 |
57 | And* newAnd() {
58 | And* and = (And*)malloc(sizeof(And));
59 | initAnd(and);
60 | return and;
61 | }
62 |
63 | void initAnd(And* and) {
64 | for (int i = 0; i < 2; i++) and->in[i] = BIT_0;
65 | initNand(&and->nand);
66 | initNot(&and->not);
67 | doAnd(and);
68 | }
69 |
70 | void doAnd(And* and) {
71 | and->nand.in[0] = and->in[0];
72 | and->nand.in[1] = and->in[1];
73 | doNand(&and->nand);
74 | and->not.in = and->nand.out;
75 | doNot(&and->not);
76 | and->out = and->not.out;
77 | }
78 |
79 | //
80 | // Or
81 | //
82 |
83 | Or* newOr() {
84 | Or* or = (Or*)malloc(sizeof(Or));
85 | initOr(or);
86 | return or;
87 | }
88 |
89 | void initOr(Or* or) {
90 | for (int i = 0; i < 2; i++) {
91 | or->in[i] = BIT_0;
92 | initNot(&or->not[i]);
93 | }
94 | initNand(&or->nand);
95 | doOr(or);
96 | }
97 |
98 | void doOr(Or* or) {
99 | or->not[0].in = or->in[0];
100 | or->not[1].in = or->in[1];
101 | doNot(&or->not[0]);
102 | doNot(&or->not[1]);
103 | or->nand.in[0] = or->not[0].out;
104 | or->nand.in[1] = or->not[1].out;
105 | doNand(&or->nand);
106 | or->out = or->nand.out;
107 | }
108 |
109 | //
110 | // Xor
111 | //
112 |
113 | Xor* newXor() {
114 | Xor* xor = (Xor*)malloc(sizeof(Xor));
115 | initXor(xor);
116 | return xor;
117 | }
118 |
119 | void initXor(Xor* xor) {
120 | for (int i = 0; i < 2; i++) xor->in[i] = BIT_0;
121 | for (int i = 0; i < 4; i++) initNand(&xor->nand[i]);
122 | doXor(xor);
123 | }
124 |
125 | void doXor(Xor* xor) {
126 | xor->nand[0].in[0] = xor->in[0];
127 | xor->nand[0].in[1] = xor->in[1];
128 | doNand(&xor->nand[0]);
129 | xor->nand[1].in[0] = xor->in[0];
130 | xor->nand[1].in[1] = xor->nand[0].out;
131 | doNand(&xor->nand[1]);
132 | xor->nand[2].in[0] = xor->in[1];
133 | xor->nand[2].in[1] = xor->nand[0].out;
134 | doNand(&xor->nand[2]);
135 | xor->nand[3].in[0] = xor->nand[1].out;
136 | xor->nand[3].in[1] = xor->nand[2].out;
137 | doNand(&xor->nand[3]);
138 | xor->out = xor->nand[3].out;
139 | }
140 |
--------------------------------------------------------------------------------
/gates.h:
--------------------------------------------------------------------------------
1 | #ifndef logic_gates_h
2 | #define logic_gates_h
3 |
4 | typedef char Bit;
5 |
6 | typedef struct Nand {
7 | Bit in[2];
8 | Bit out;
9 | } Nand;
10 |
11 | Nand *newNand();
12 | void initNand(Nand *nand);
13 | void doNand(Nand *nand);
14 |
15 | typedef struct Not {
16 | Bit in;
17 | Nand nand;
18 | Bit out;
19 | } Not;
20 |
21 | Not *newNot();
22 | void initNot(Not * not);
23 | void doNot(Not * not);
24 |
25 | typedef struct And {
26 | Bit in[2];
27 | Nand nand;
28 | Not not;
29 | Bit out;
30 | } And;
31 |
32 | And *newAnd();
33 | void initAnd(And *and);
34 | void doAnd(And *and);
35 |
36 | typedef struct Or {
37 | Bit in[2];
38 | Nand nand;
39 | Not not[2];
40 | Bit out;
41 | } Or;
42 |
43 | Or *newOr();
44 | void initOr(Or *or );
45 | void doOr(Or *or );
46 |
47 | typedef struct Xor {
48 | Bit in[2];
49 | Nand nand[4];
50 | Bit out;
51 | } Xor;
52 |
53 | Xor *newXor();
54 | void initXor(Xor *xor);
55 | void doXor(Xor *xor);
56 |
57 | #endif
58 |
--------------------------------------------------------------------------------
/main.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 |
4 | #include "gates.h"
5 | #include "arithmetic.h"
6 | #include "components.h"
7 | #include "test.h"
8 |
9 | int main(int argc, char* argv[]) {
10 | test();
11 | return 0;
12 | }
13 |
--------------------------------------------------------------------------------
/memory.c:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | #include "gates.h"
4 | #include "arithmetic.h"
5 | #include "components.h"
6 | #include "ram.h"
7 | #include "memory.h"
8 |
9 | //
10 | // Memory
11 | //
12 |
13 | Memory* newMemory() {
14 | Memory* memory = (Memory*)malloc(sizeof(Memory));
15 | initMemory(memory);
16 | return memory;
17 | }
18 |
19 | void initMemory(Memory* mem) {
20 | int i;
21 | mem->clock = '0';
22 | for (i = 0; i < 8; i++) mem->data[i] = '0';
23 | for (i = 0; i < 3; i++) {
24 | mem->store[i] = '0';
25 | initRegister(&mem->reg[i]);
26 | }
27 | initRAM256(&mem->ram);
28 | doMemory(mem);
29 | }
30 |
31 | void doMemory(Memory* mem) {
32 | int i, j;
33 | for (i = 0; i < 3; i++) {
34 | mem->reg[i].store = mem->store[i];
35 | mem->reg[i].clock = mem->clock;
36 | for (j = 0; j < 8; j++) mem->reg[i].data[j] = mem->data[j];
37 | doRegister(&mem->reg[i]);
38 | }
39 |
40 | mem->ram.store = mem->store[3];
41 | mem->ram.clock = mem->clock;
42 | for (i = 0; i < 8; i++) {
43 | mem->ram.data[i] = mem->data[i];
44 | mem->ram.address[i] = mem->reg[0].out[i];
45 | }
46 | doRAM256(&mem->ram);
47 | for (i = 0; i < 3; i++) {
48 | for (j = 0; j < 8; j++) mem->out[i][j] = mem->reg[i].out[j];
49 | }
50 | for (i = 0; i < 8; i++) mem->out[3][i] = mem->ram.out[i];
51 | }
52 |
--------------------------------------------------------------------------------
/memory.h:
--------------------------------------------------------------------------------
1 | #ifndef logic_memory_h
2 | #define logic_memory_h
3 |
4 | typedef struct Memory {
5 | Bit store[4]; // address, data, ALU, RAM;
6 | Bit clock;
7 | Bit data[8];
8 | Register reg[3]; // address, data, ALU;
9 | RAM256 ram;
10 | Bit out[4][8]; // address, data, ALU, RAM;
11 | } Memory;
12 |
13 | Memory* newMemory();
14 | void initMemory(Memory* mem);
15 | void doMemory(Memory* mem);
16 |
17 | #endif
18 |
--------------------------------------------------------------------------------
/ram.c:
--------------------------------------------------------------------------------
1 | #include "gates.h"
2 | #include "arithmetic.h"
3 | #include
4 | #include
5 |
6 | #include "components.h"
7 | #include "types.h"
8 | #include "ram.h"
9 |
10 | //
11 | // RAM
12 | //
13 |
14 | RAM* newRAM() {
15 | RAM* ram = (RAM*)malloc(sizeof(RAM));
16 | initRAM(ram);
17 | return ram;
18 | }
19 |
20 | void initRAM(RAM* ram) {
21 | int i;
22 | ram->store = BIT_0;
23 | ram->clock = BIT_0;
24 | ram->address = BIT_0;
25 | for (i = 0; i < 8; i++) ram->data[i] = BIT_0;
26 | initDMux(&ram->dmux);
27 | for (i = 0; i < 2; i++) initRegister(&ram->reg[i]);
28 | initMux8(&ram->mux8);
29 | doRAM(ram);
30 | }
31 |
32 | void doRAM(RAM* ram) {
33 | int i, j;
34 | ram->dmux.store = ram->address;
35 | ram->dmux.data = ram->store;
36 | doDMux(&ram->dmux);
37 | for (i = 0; i < 2; i++) {
38 | ram->reg[i].store = ram->dmux.out[1 - i];
39 | for (j = 0; j < 8; j++) ram->reg[i].data[j] = ram->data[j];
40 | ram->reg[i].clock = ram->clock;
41 | doRegister(&ram->reg[i]);
42 | for (j = 0; j < 8; j++) ram->mux8.data[i][j] = ram->reg[1 - i].out[j];
43 | }
44 | ram->mux8.store = ram->address;
45 | doMux8(&ram->mux8);
46 | for (i = 0; i < 8; i++) ram->out[i] = ram->mux8.out[i];
47 | }
48 |
49 | //
50 | // RAM4
51 | //
52 |
53 | RAM4* newRAM4() {
54 | RAM4* ram = (RAM4*)malloc(sizeof(RAM4));
55 | initRAM4(ram);
56 | return ram;
57 | }
58 |
59 | void initRAM4(RAM4* ram4) {
60 | int i;
61 | ram4->store = BIT_0;
62 | ram4->clock = BIT_0;
63 | for (i = 0; i < 2; i++) ram4->address[i] = BIT_0;
64 | for (i = 0; i < 8; i++) ram4->data[i] = BIT_0;
65 | initDMux(&ram4->dmux);
66 | for (i = 0; i < 2; i++) initRAM(&ram4->ram[i]);
67 | initMux8(&ram4->mux8);
68 | doRAM4(ram4);
69 | }
70 |
71 | void doRAM4(RAM4* ram4) {
72 | int i, j;
73 | ram4->dmux.store = ram4->address[0];
74 | ram4->dmux.data = ram4->store;
75 | doDMux(&ram4->dmux);
76 | for (i = 0; i < 2; i++) {
77 | ram4->ram[i].address = ram4->address[1];
78 | ram4->ram[i].store = ram4->dmux.out[1 - i];
79 | for (j = 0; j < 8; j++) ram4->ram[i].data[j] = ram4->data[j];
80 | ram4->ram[i].clock = ram4->clock;
81 | doRAM(&ram4->ram[i]);
82 | for (j = 0; j < 8; j++) ram4->mux8.data[i][j] = ram4->ram[1 - i].out[j];
83 | }
84 | ram4->mux8.store = ram4->address[0];
85 | doMux8(&ram4->mux8);
86 | for (i = 0; i < 8; i++) ram4->out[i] = ram4->mux8.out[i];
87 | }
88 |
89 | //
90 | // RAM8
91 | //
92 |
93 | RAM8* newRAM8() {
94 | RAM8* ram = (RAM8*)malloc(sizeof(RAM8));
95 | initRAM8(ram);
96 | return ram;
97 | }
98 |
99 | void initRAM8(RAM8* ram8) {
100 | int i;
101 | ram8->store = BIT_0;
102 | ram8->clock = BIT_0;
103 | for (i = 0; i < 3; i++) ram8->address[i] = BIT_0;
104 | for (i = 0; i < 8; i++) ram8->data[i] = BIT_0;
105 | initDMux(&ram8->dmux);
106 | for (i = 0; i < 2; i++) initRAM4(&ram8->ram4[i]);
107 | initMux8(&ram8->mux8);
108 | doRAM8(ram8);
109 | }
110 |
111 | void doRAM8(RAM8* ram8) {
112 | int i, j;
113 | ram8->dmux.store = ram8->address[0];
114 | ram8->dmux.data = ram8->store;
115 | doDMux(&ram8->dmux);
116 | for (i = 0; i < 2; i++) {
117 | for (j = 0; j < 2; j++) ram8->ram4[i].address[j] = ram8->address[j + 1];
118 | ram8->ram4[i].store = ram8->dmux.out[1 - i];
119 | for (j = 0; j < 8; j++) ram8->ram4[i].data[j] = ram8->data[j];
120 | ram8->ram4[i].clock = ram8->clock;
121 | doRAM4(&ram8->ram4[i]);
122 | for (j = 0; j < 8; j++) ram8->mux8.data[i][j] = ram8->ram4[1 - i].out[j];
123 | }
124 | ram8->mux8.store = ram8->address[0];
125 | doMux8(&ram8->mux8);
126 | for (i = 0; i < 8; i++) ram8->out[i] = ram8->mux8.out[i];
127 | }
128 |
129 | //
130 | // RAM16
131 | //
132 |
133 | RAM16* newRAM16() {
134 | RAM16* ram = (RAM16*)malloc(sizeof(RAM16));
135 | initRAM16(ram);
136 | return ram;
137 | }
138 |
139 | void initRAM16(RAM16* ram16) {
140 | int i;
141 | ram16->store = BIT_0;
142 | ram16->clock = BIT_0;
143 | for (i = 0; i < 4; i++) ram16->address[i] = BIT_0;
144 | for (i = 0; i < 8; i++) ram16->data[i] = BIT_0;
145 | initDMux(&ram16->dmux);
146 | for (i = 0; i < 2; i++) initRAM8(&ram16->ram8[i]);
147 | initMux8(&ram16->mux8);
148 | doRAM16(ram16);
149 | }
150 |
151 | void doRAM16(RAM16* ram16) {
152 | int i, j;
153 | ram16->dmux.store = ram16->address[0];
154 | ram16->dmux.data = ram16->store;
155 | doDMux(&ram16->dmux);
156 | for (i = 0; i < 2; i++) {
157 | for (j = 0; j < 3; j++) ram16->ram8[i].address[j] = ram16->address[j + 1];
158 | ram16->ram8[i].store = ram16->dmux.out[1 - i];
159 | for (j = 0; j < 8; j++) ram16->ram8[i].data[j] = ram16->data[j];
160 | ram16->ram8[i].clock = ram16->clock;
161 | doRAM8(&ram16->ram8[i]);
162 | for (j = 0; j < 8; j++) ram16->mux8.data[i][j] = ram16->ram8[1 - i].out[j];
163 | }
164 | ram16->mux8.store = ram16->address[0];
165 | doMux8(&ram16->mux8);
166 | for (i = 0; i < 8; i++) ram16->out[i] = ram16->mux8.out[i];
167 | }
168 |
169 | //
170 | // RAM32
171 | //
172 |
173 | RAM32* newRAM32() {
174 | RAM32* ram = (RAM32*)malloc(sizeof(RAM32));
175 | initRAM32(ram);
176 | return ram;
177 | }
178 |
179 | void initRAM32(RAM32* ram32) {
180 | int i;
181 | ram32->store = BIT_0;
182 | ram32->clock = BIT_0;
183 | for (i = 0; i < 5; i++) ram32->address[i] = BIT_0;
184 | for (i = 0; i < 8; i++) ram32->data[i] = BIT_0;
185 | initDMux(&ram32->dmux);
186 | for (i = 0; i < 2; i++) initRAM16(&ram32->ram16[i]);
187 | initMux8(&ram32->mux8);
188 | doRAM32(ram32);
189 | }
190 |
191 | void doRAM32(RAM32* ram32) {
192 | int i, j;
193 | ram32->dmux.store = ram32->address[0];
194 | ram32->dmux.data = ram32->store;
195 | doDMux(&ram32->dmux);
196 | for (i = 0; i < 2; i++) {
197 | for (j = 0; j < 4; j++) ram32->ram16[i].address[j] = ram32->address[j + 1];
198 | ram32->ram16[i].store = ram32->dmux.out[1 - i];
199 | for (j = 0; j < 8; j++) ram32->ram16[i].data[j] = ram32->data[j];
200 | ram32->ram16[i].clock = ram32->clock;
201 | doRAM16(&ram32->ram16[i]);
202 | for (j = 0; j < 8; j++) ram32->mux8.data[i][j] = ram32->ram16[1 - i].out[j];
203 | }
204 | ram32->mux8.store = ram32->address[0];
205 | doMux8(&ram32->mux8);
206 | for (i = 0; i < 8; i++) ram32->out[i] = ram32->mux8.out[i];
207 | }
208 |
209 | //
210 | // RAM64
211 | //
212 |
213 | RAM64* newRAM64() {
214 | RAM64* ram = (RAM64*)malloc(sizeof(RAM64));
215 | initRAM64(ram);
216 | return ram;
217 | }
218 |
219 | void initRAM64(RAM64* ram64) {
220 | int i;
221 | ram64->store = BIT_0;
222 | ram64->clock = BIT_0;
223 | for (i = 0; i < 6; i++) ram64->address[i] = BIT_0;
224 | for (i = 0; i < 8; i++) ram64->data[i] = BIT_0;
225 | initDMux(&ram64->dmux);
226 | for (i = 0; i < 2; i++) initRAM32(&ram64->ram32[i]);
227 | initMux8(&ram64->mux8);
228 | doRAM64(ram64);
229 | }
230 |
231 | void doRAM64(RAM64* ram64) {
232 | int i, j;
233 | ram64->dmux.store = ram64->address[0];
234 | ram64->dmux.data = ram64->store;
235 | doDMux(&ram64->dmux);
236 | for (i = 0; i < 2; i++) {
237 | for (j = 0; j < 5; j++) ram64->ram32[i].address[j] = ram64->address[j + 1];
238 | ram64->ram32[i].store = ram64->dmux.out[1 - i];
239 | for (j = 0; j < 8; j++) ram64->ram32[i].data[j] = ram64->data[j];
240 | ram64->ram32[i].clock = ram64->clock;
241 | doRAM32(&ram64->ram32[i]);
242 | for (j = 0; j < 8; j++) ram64->mux8.data[i][j] = ram64->ram32[1 - i].out[j];
243 | }
244 | ram64->mux8.store = ram64->address[0];
245 | doMux8(&ram64->mux8);
246 | for (i = 0; i < 8; i++) ram64->out[i] = ram64->mux8.out[i];
247 | }
248 |
249 | //
250 | // RAM128
251 | //
252 |
253 | RAM128* newRAM128() {
254 | RAM128* ram = (RAM128*)malloc(sizeof(RAM128));
255 | initRAM128(ram);
256 | return ram;
257 | }
258 |
259 | void initRAM128(RAM128* ram128) {
260 | int i;
261 | ram128->store = BIT_0;
262 | ram128->clock = BIT_0;
263 | for (i = 0; i < 7; i++) ram128->address[i] = BIT_0;
264 | for (i = 0; i < 8; i++) ram128->data[i] = BIT_0;
265 | initDMux(&ram128->dmux);
266 | for (i = 0; i < 2; i++) initRAM64(&ram128->ram64[i]);
267 | initMux8(&ram128->mux8);
268 | doRAM128(ram128);
269 | }
270 |
271 | void doRAM128(RAM128* ram128) {
272 | int i, j;
273 | ram128->dmux.store = ram128->address[0];
274 | ram128->dmux.data = ram128->store;
275 | doDMux(&ram128->dmux);
276 | for (i = 0; i < 2; i++) {
277 | for (j = 0; j < 6; j++) ram128->ram64[i].address[j] = ram128->address[j + 1];
278 | ram128->ram64[i].store = ram128->dmux.out[1 - i];
279 | for (j = 0; j < 8; j++) ram128->ram64[i].data[j] = ram128->data[j];
280 | ram128->ram64[i].clock = ram128->clock;
281 | doRAM64(&ram128->ram64[i]);
282 | for (j = 0; j < 8; j++) ram128->mux8.data[i][j] = ram128->ram64[1 - i].out[j];
283 | }
284 | ram128->mux8.store = ram128->address[0];
285 | doMux8(&ram128->mux8);
286 | for (i = 0; i < 8; i++) ram128->out[i] = ram128->mux8.out[i];
287 | }
288 |
289 | //
290 | // RAM256
291 | //
292 |
293 | RAM256* newRAM256() {
294 | RAM256* ram = (RAM256*)malloc(sizeof(RAM256));
295 | initRAM256(ram);
296 | return ram;
297 | }
298 |
299 | void initRAM256(RAM256* ram256) {
300 | int i;
301 | ram256->store = BIT_0;
302 | ram256->clock = BIT_0;
303 | for (i = 0; i < 8; i++) ram256->address[i] = BIT_0;
304 | for (i = 0; i < 8; i++) ram256->data[i] = BIT_0;
305 | initDMux(&ram256->dmux);
306 | for (i = 0; i < 2; i++) initRAM128(&ram256->ram128[i]);
307 | initMux8(&ram256->mux8);
308 | doRAM256(ram256);
309 | }
310 |
311 | void doRAM256(RAM256* ram256) {
312 | int i, j;
313 | ram256->dmux.store = ram256->address[0];
314 | ram256->dmux.data = ram256->store;
315 | doDMux(&ram256->dmux);
316 | for (i = 0; i < 2; i++) {
317 | for (j = 0; j < 7; j++) ram256->ram128[i].address[j] = ram256->address[j + 1];
318 | ram256->ram128[i].store = ram256->dmux.out[1 - i];
319 | for (j = 0; j < 8; j++) ram256->ram128[i].data[j] = ram256->data[j];
320 | ram256->ram128[i].clock = ram256->clock;
321 | doRAM128(&ram256->ram128[i]);
322 | for (j = 0; j < 8; j++) ram256->mux8.data[i][j] = ram256->ram128[1 - i].out[j];
323 | }
324 | ram256->mux8.store = ram256->address[0];
325 | doMux8(&ram256->mux8);
326 | for (i = 0; i < 8; i++) ram256->out[i] = ram256->mux8.out[i];
327 | }
328 |
--------------------------------------------------------------------------------
/ram.h:
--------------------------------------------------------------------------------
1 | #ifndef logic_ram_h
2 | #define logic_ram_h
3 |
4 | typedef struct RAM {
5 | Bit store, address, clock;
6 | Bit data[8];
7 | DMux dmux;
8 | Register reg[2];
9 | Mux8 mux8;
10 | Bit out[8];
11 | } RAM;
12 |
13 | RAM *newRAM();
14 | void initRAM(RAM *ram);
15 | void doRAM(RAM *ram);
16 |
17 | typedef struct RAM4 {
18 | Bit store, address[2], clock;
19 | Bit data[8];
20 | DMux dmux;
21 | RAM ram[2];
22 | Mux8 mux8;
23 | Bit out[8];
24 | } RAM4;
25 |
26 | RAM4 *newRAM4();
27 | void initRAM4(RAM4 *ram4);
28 | void doRAM4(RAM4 *ram4);
29 |
30 | typedef struct RAM8 {
31 | Bit store, address[3], clock;
32 | Bit data[8];
33 | DMux dmux;
34 | RAM4 ram4[2];
35 | Mux8 mux8;
36 | Bit out[8];
37 | } RAM8;
38 |
39 | RAM8 *newRAM8();
40 | void initRAM8(RAM8 *ram8);
41 | void doRAM8(RAM8 *ram8);
42 |
43 | typedef struct RAM16 {
44 | Bit store, address[4], clock;
45 | Bit data[8];
46 | DMux dmux;
47 | RAM8 ram8[2];
48 | Mux8 mux8;
49 | Bit out[8];
50 | } RAM16;
51 |
52 | RAM16 *newRAM16();
53 | void initRAM16(RAM16 *ram16);
54 | void doRAM16(RAM16 *ram16);
55 |
56 | typedef struct RAM32 {
57 | Bit store, address[5], clock;
58 | Bit data[8];
59 | DMux dmux;
60 | RAM16 ram16[2];
61 | Mux8 mux8;
62 | Bit out[8];
63 | } RAM32;
64 |
65 | RAM32 *newRAM32();
66 | void initRAM32(RAM32 *ram32);
67 | void doRAM32(RAM32 *ram32);
68 |
69 | typedef struct RAM64 {
70 | Bit store, address[6], clock;
71 | Bit data[8];
72 | DMux dmux;
73 | RAM32 ram32[2];
74 | Mux8 mux8;
75 | Bit out[8];
76 | } RAM64;
77 |
78 | RAM64 *newRAM64();
79 | void initRAM64(RAM64 *ram64);
80 | void doRAM64(RAM64 *ram64);
81 |
82 | typedef struct RAM128 {
83 | Bit store, address[7], clock;
84 | Bit data[8];
85 | DMux dmux;
86 | RAM64 ram64[2];
87 | Mux8 mux8;
88 | Bit out[8];
89 | } RAM128;
90 |
91 | RAM128 *newRAM128();
92 | void initRAM128(RAM128 *ram128);
93 | void doRAM128(RAM128 *ram128);
94 |
95 | typedef struct RAM256 {
96 | Bit store, address[8], clock;
97 | Bit data[8];
98 | DMux dmux;
99 | RAM128 ram128[2];
100 | Mux8 mux8;
101 | Bit out[8];
102 | } RAM256;
103 |
104 | RAM256* newRAM256();
105 | void initRAM256(RAM256* ram256);
106 | void doRAM256(RAM256* ram256);
107 |
108 | #endif
109 |
--------------------------------------------------------------------------------
/test.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 | #include
5 |
6 | #include "gates.h"
7 | #include "arithmetic.h"
8 | #include "components.h"
9 | #include "ram.h"
10 | #include "alu.h"
11 | #include "memory.h"
12 | #include "test.h"
13 |
14 | void testNand() {
15 | Bit i, j;
16 | Nand* nand = newNand();
17 |
18 | puts("Testing nand");
19 |
20 | for (i = '0'; i < '2'; i++) {
21 | for (j = '0'; j < '2'; j++) {
22 | nand->in[0] = i; nand->in[1] = j;
23 | doNand(nand);
24 | printf("%c nand %c = %c\n", i, j, nand->out);
25 | }
26 | }
27 |
28 | puts("");
29 |
30 | free(nand);
31 | }
32 |
33 | void testNot() {
34 | Bit i;
35 | Not* not = newNot();
36 |
37 | puts("Testing not");
38 |
39 | for (i = '0'; i < '2'; i++) {
40 | not->in = i;
41 | doNot(not);
42 | printf("not %c = %c\n", i, not->out);
43 | }
44 |
45 | puts("");
46 |
47 | free(not);
48 | }
49 |
50 | void testAnd() {
51 | Bit i, j;
52 | And* and = newAnd();
53 |
54 | puts("Testing and");
55 |
56 | for (i = '0'; i < '2'; i++) {
57 | for (j = '0'; j < '2'; j++) {
58 | and->in[0] = i; and->in[1] = j;
59 | doAnd(and);
60 | printf("%c and %c = %c\n", i, j, and->out);
61 | }
62 | }
63 |
64 | puts("");
65 |
66 | free(and);
67 | }
68 |
69 | void testOr() {
70 | Bit i, j;
71 | Or* or = newOr();
72 |
73 | puts("Testing or");
74 |
75 | for (i = '0'; i < '2'; i++) {
76 | for (j = '0'; j < '2'; j++) {
77 | or->in[0] = i; or->in[1] = j;
78 | doOr(or);
79 | printf("%c or %c = %c\n", i, j, or->out);
80 | }
81 | }
82 |
83 | puts("");
84 |
85 | free(or);
86 | }
87 |
88 | void testXor() {
89 | Bit i, j;
90 | Xor* xor = newXor();
91 |
92 | puts("Testing xor");
93 |
94 | for (i = '0'; i < '2'; i++) {
95 | for (j = '0'; j < '2'; j++) {
96 | xor->in[0] = i; xor->in[1] = j;
97 | doXor(xor);
98 | printf("%c xor %c = %c\n", i, j, xor->out);
99 | }
100 | }
101 |
102 | puts("");
103 |
104 | free(xor);
105 | }
106 |
107 | void testHalfAdd() {
108 | Bit i, j;
109 | HalfAdd* halfAdd = (HalfAdd*)malloc(sizeof(HalfAdd));
110 | initHalfAdd(halfAdd);
111 |
112 | puts("Testing half add");
113 |
114 | for (i = '0'; i < '2'; i++) {
115 | for (j = '0'; j < '2'; j++) {
116 | halfAdd->in[0] = i; halfAdd->in[1] = j;
117 | doHalfAdd(halfAdd);
118 | printf("%c + %c = %s\n", i, j, halfAdd->out);
119 | }
120 | }
121 |
122 | puts("");
123 |
124 | free(halfAdd);
125 | }
126 |
127 | void testFullAdd() {
128 | Bit i, j, k;
129 | FullAdd* fullAdd = (FullAdd*)malloc(sizeof(FullAdd));
130 | initFullAdd(fullAdd);
131 |
132 | puts("Testing full add");
133 |
134 | for (i = '0'; i < '2'; i++) {
135 | for (j = '0'; j < '2'; j++) {
136 | for (k = '0'; k < '2'; k++) {
137 | fullAdd->in[0] = i; fullAdd->in[1] = j; fullAdd->in[2] = k;
138 | doFullAdd(fullAdd);
139 | printf("%c + %c + %c = %c%c\n", i, j, k, fullAdd->out[0], fullAdd->out[1]);
140 | }
141 | }
142 | }
143 |
144 | puts("");
145 |
146 | free(fullAdd);
147 | }
148 |
149 | void testAdd() {
150 | int i, j;
151 | Add* add = (Add*)malloc(sizeof(Add));
152 | initAdd(add);
153 |
154 | puts("testing 8-bit add");
155 |
156 | Bit in[2][8] = { "00000111", "00000001" };
157 |
158 | for (i = 0; i < 2; i++) {
159 | for (j = 0; j < 8; j++) {
160 | add->in[i][j] = in[i][j];
161 | }
162 | }
163 |
164 | add->carryIn = '0';
165 | doAdd(add);
166 |
167 | for (i = 0; i < 8; i++) printf("%c", add->in[0][i]);
168 | printf(" + ");
169 | for (i = 0; i < 8; i++) printf("%c", add->in[1][i]);
170 | printf(" + %c = [%c]", add->carryIn, add->carryOut);
171 | for (i = 0; i < 8; i++) printf("%c", add->out[i]);
172 | puts("");
173 |
174 | free(add);
175 | }
176 |
177 | void testIncrement() {
178 | int i;
179 | Increment* increment = (Increment*)malloc(sizeof(Increment));
180 | initIncrement(increment);
181 |
182 | puts("Testing increment");
183 |
184 | increment->in[7] = '1';
185 |
186 | doIncrement(increment);
187 |
188 | printf("increment(");
189 | for (i = 0; i < 8; i++) printf("%c", increment->in[i]);
190 | printf(") = ");
191 | for (i = 0; i < 8; i++) printf("%c", increment->out[i]);
192 |
193 | puts("");
194 |
195 | free(increment);
196 | }
197 |
198 | void testSubtract() {
199 | int i;
200 | Subtract* subtract = (Subtract*)malloc(sizeof(Subtract));
201 | initSubtract(subtract);
202 |
203 | puts("Testing subtract");
204 |
205 |
206 | subtract->in[1][7] = '1';
207 | doSubtract(subtract);
208 |
209 | for (i = 0; i < 8; i++) printf("%c", subtract->in[0][i]);
210 | printf(" - ");
211 | for (i = 0; i < 8; i++) printf("%c", subtract->in[1][i]);
212 | printf(" = ");
213 | for (i = 0; i < 8; i++) printf("%c", subtract->out[i]);
214 | puts("");
215 |
216 | free(subtract);
217 | }
218 |
219 | void testEqualsZero() {
220 | int i;
221 | EqualsZero* equalsZero = (EqualsZero*)malloc(sizeof(EqualsZero));
222 |
223 | puts("Testing equals zero");
224 |
225 | for (i = 0; i < 8; i++) equalsZero->in[i] = '0';
226 | doEqualsZero(equalsZero);
227 |
228 | for (i = 0; i < 8; i++) printf("%c", equalsZero->in[i]);
229 | printf(" = 0 ? %c\n", equalsZero->out);
230 |
231 | equalsZero->in[7] = '1';
232 | doEqualsZero(equalsZero);
233 |
234 | for (i = 0; i < 8; i++) printf("%c", equalsZero->in[i]);
235 | printf(" = 0 ? %c\n", equalsZero->out);
236 |
237 | free(equalsZero);
238 | }
239 |
240 | void testLessThanZero() {
241 | int i;
242 | LessThanZero* lessThanZero = (LessThanZero*)malloc(sizeof(LessThanZero*));
243 | initLessThanZero(lessThanZero);
244 |
245 | puts("Testing less than zero");
246 |
247 | for (i = 0; i < 8; i++) lessThanZero->in[i] = '1';
248 |
249 | doLessThanZero(lessThanZero);
250 |
251 | printf("-1 < 0 ? %c\n", lessThanZero->out);
252 |
253 | free(lessThanZero);
254 | }
255 |
256 | void testMux() {
257 | Bit i, j, k;
258 | Mux* mux = (Mux*)malloc(sizeof(Mux));
259 | initMux(mux);
260 |
261 | puts("Testing mux");
262 |
263 | for (i = '0'; i < '2'; i++) {
264 | for (j = '0'; j < '2'; j++) {
265 | for (k = '0'; k < '2'; k++) {
266 | mux->store = i; mux->data[0] = j; mux->data[1] = k;
267 | doMux(mux);
268 | printf("%c %c %c = %c\n", i, j, k, mux->out);
269 | }
270 | }
271 | }
272 |
273 | puts("");
274 |
275 | free(mux);
276 | }
277 |
278 | void testMux8() {
279 | int i;
280 | Mux8* mux8 = (Mux8*)malloc(sizeof(Mux8));
281 | initMux8(mux8);
282 |
283 | printf("Testing 8-bit mux\n");
284 |
285 | printf("store: %c\n", mux8->store);
286 | printf("byte 0: ");
287 | for (i = 0; i < 8; i++) printf("%c", mux8->data[0][i]);
288 | printf("\nbyte 1: ");
289 | for (i = 0; i < 8; i++) printf("%c", mux8->data[1][i]);
290 | printf("\nmuxs:\n");
291 | for (i = 0; i < 8; i++) {
292 | printf("%d: store: %c in: %c%c out: %c\n", i, mux8->mux[i].store, mux8->mux[i].data[0], mux8->mux[i].data[1], mux8->mux[i].out);
293 | }
294 | printf("\noutput: ");
295 | for (i = 0; i < 8; i++) printf("%c", mux8->out[i]);
296 | printf("\n");
297 | // initMux(mux);
298 | for (i = 0; i < 8; i++) printf("%c\n", mux8->out[i]);
299 | puts("Testing 8-bit mux");
300 |
301 | mux8->data[0][7] = '1';
302 | doMux8(mux8);
303 | for (i = 0; i < 8; i++) printf("%c", mux8->out[i]);
304 | puts("");
305 | mux8->store = '1';
306 | doMux8(mux8);
307 | for (i = 0; i < 8; i++) printf("%c", mux8->out[i]);
308 | puts("");
309 | }
310 |
311 | void testDMux() {
312 | Bit i, j;
313 | DMux* dmux = (DMux*)malloc(sizeof(DMux));
314 | initDMux(dmux);
315 |
316 | puts("Testing dmux");
317 |
318 | for (i = '0'; i < '2'; i++) {
319 | for (j = '0'; j < '2'; j++) {
320 | dmux->store = i; dmux->data = j;
321 | doDMux(dmux);
322 | printf("%c %c = %c%c\n", i, j, dmux->out[0], dmux->out[1]);
323 | }
324 | }
325 |
326 | puts("");
327 |
328 | free(dmux);
329 | }
330 |
331 | void testLatch() {
332 | Bit i, j;
333 | Latch* latch = (Latch*)malloc(sizeof(Latch));
334 | initLatch(latch);
335 |
336 | puts("Testing latch");
337 |
338 | for (i = '1'; i > '0' - 1; --i) {
339 | for (j = '0'; j < '2'; j++) {
340 | latch->store = i; latch->data = j;
341 | doLatch(latch);
342 | printf("%c %c = %c\n", i, j, latch->out);
343 | }
344 | }
345 |
346 | puts("");
347 |
348 | free(latch);
349 | }
350 |
351 | void testDFF() {
352 | Bit i, j, k;
353 | DFF* dff = (DFF*)malloc(sizeof(DFF));
354 | initDFF(dff);
355 |
356 | puts("Testing flip flop");
357 |
358 | for (i = '1'; i > '0' - 1; --i) {
359 | for (j = '0'; j < '2'; j++) {
360 | for (k = '0'; k < '2'; k++) {
361 | dff->store = i; dff->data = j; dff->clock = k;
362 | doDFF(dff);
363 | printf("%c %c %c = %c\n", i, j, k, dff->out);
364 | }
365 | }
366 | }
367 |
368 | puts("");
369 |
370 | free(dff);
371 | }
372 |
373 | void testRegister() {
374 | int i;
375 | Register* reg = (Register*)malloc(sizeof(Register));
376 | initRegister(reg);
377 | puts("Testing register");
378 | reg->store = '1';
379 | for (i = 0; i < 8; i++) {
380 | reg->data[i] = (i % 2)+'0';
381 | printf("%c", reg->out[i]);
382 | }
383 | doRegister(reg);
384 | reg->clock = '1';
385 | doRegister(reg);
386 | puts("");
387 | for (i = 0; i < 8; i++) printf("%c", reg->out[i]);
388 | puts("");
389 | }
390 |
391 | void testCounter() {
392 | int i, j;
393 | Counter* counter = (Counter*)malloc(sizeof(Counter));
394 | initCounter(counter);
395 |
396 | puts("Testing counter");
397 | for (i=0;i < 256 * 2;i++) {
398 | if (counter->clock == '1') {
399 | for (j = 0; j < 8; j++) printf("%c", counter->out[j]);
400 | puts("");
401 | }
402 | counter->clock = (i % 2) + '0';
403 | doCounter(counter);
404 | }
405 | free(counter);
406 | }
407 |
408 | void testRAM() {
409 | int i;
410 | RAM* ram = (RAM*)malloc(sizeof(RAM));
411 | initRAM(ram);
412 |
413 | for (i = 0; i < 8; i++) ram->data[i] = '1';
414 | doRAM(ram);
415 | puts("Testing RAM");
416 |
417 | // ram->address = '1';
418 | ram->store = '1';
419 | doRAM(ram);
420 | ram->clock = '1';
421 | doRAM(ram);
422 | ram->store = '0';
423 | doRAM(ram);
424 | ram->clock = '0';
425 | doRAM(ram);
426 |
427 | printf("RAM out: ");
428 | for (i = 0; i < 8; i++) printf("%c", ram->out[i]);
429 | printf("\nReg 0 out: ");
430 | for (i = 0; i < 8; i++) printf("%c", ram->reg[0].out[i]);
431 | printf("\nReg 1 out: ");
432 | for (i = 0; i < 8; i++) printf("%c", ram->reg[1].out[i]);
433 |
434 |
435 | for (i = 0; i < 8; i++) ram->data[i] = (i % 2) + '0';
436 | doRAM(ram);
437 | ram->address = '1';
438 | doRAM(ram);
439 | ram->store = '1';
440 | doRAM(ram);
441 | ram->clock = '1';
442 | doRAM(ram);
443 | ram->store = '0';
444 | doRAM(ram);
445 | ram->clock = '0';
446 | doRAM(ram);
447 | printf("\n\nRAM out: ");
448 | for (i = 0; i < 8; i++) printf("%c", ram->out[i]);
449 | printf("\nReg 0 out: ");
450 | for (i = 0; i < 8; i++) printf("%c", ram->reg[0].out[i]);
451 | printf("\nReg 1 out: ");
452 | for (i = 0; i < 8; i++) printf("%c", ram->reg[1].out[i]);
453 |
454 | ram->address = '0';
455 | doRAM(ram);
456 |
457 | printf("\n\nRAM out: ");
458 | for (i = 0; i < 8; i++) printf("%c", ram->out[i]);
459 | printf("\nReg 0 out: ");
460 | for (i = 0; i < 8; i++) printf("%c", ram->reg[0].out[i]);
461 | printf("\nReg 1 out: ");
462 | for (i = 0; i < 8; i++) printf("%c", ram->reg[1].out[i]);
463 | puts("");
464 | free(ram);
465 | }
466 |
467 | void testUnary() {
468 | int i, j, k;
469 | Unary* unary = (Unary*)malloc(sizeof(Unary));
470 | initUnary(unary);
471 |
472 | for (i = 0; i < 8; i++) unary->data[i] = (i % 2) + '0';
473 |
474 | for (i = '0'; i < '2'; i++) {
475 | for (j = '0'; j < '2'; j++) {
476 | unary->zero = i;
477 | unary->negate = j;
478 | doUnary(unary);
479 | printf("z: %c n: %c out: ", i, j);
480 | for (k = 0; k < 8; k++) printf("%c", unary->out[k]);
481 | puts("");
482 | }
483 | }
484 | free(unary);
485 | }
486 |
487 | void testMemory() {
488 | int i, j;
489 | Memory* mem = (Memory*)malloc(sizeof(Memory));
490 | initMemory(mem);
491 |
492 | mem->store[0] = '1';
493 |
494 | for (i = 0; i < 8; i++) mem->data[i] = '1';
495 | doMemory(mem);
496 | mem->clock = '1';
497 | doMemory(mem);
498 | // mem->store[0] = '1';
499 | mem->store[3] = '1';
500 | for (i = 0; i < 8; i++) mem->data[i] = '0';
501 | doMemory(mem);
502 | mem->clock = '0';
503 | doMemory(mem);
504 | mem->store[0] = '0';
505 | doMemory(mem);
506 | mem->clock = '0';
507 | doMemory(mem);
508 |
509 | for (i = 0; i < 4; i++) {
510 | for (j = 0; j < 8; j++) printf("%c", mem->out[i][j]);
511 | puts("");
512 | }
513 |
514 | free(mem);
515 | }
516 |
517 | void test() {
518 | testNand();
519 | testNot();
520 | testAnd();
521 | testOr();
522 | testXor();
523 | testHalfAdd();
524 | testFullAdd();
525 | testAdd();
526 | testIncrement();
527 | testSubtract();
528 | testEqualsZero();
529 | testLessThanZero();
530 | testMux();
531 | testMux8();
532 | testDMux();
533 | testLatch();
534 | testDFF();
535 | testRegister();
536 | testCounter();
537 | testRAM();
538 | testUnary();
539 | testMemory();
540 | }
541 |
--------------------------------------------------------------------------------
/test.h:
--------------------------------------------------------------------------------
1 | #ifndef logic_test_h
2 | #define logic_test_h
3 |
4 | void test();
5 |
6 | #endif
7 |
--------------------------------------------------------------------------------
/types.h:
--------------------------------------------------------------------------------
1 | #define ARCHITECTURE_BITS 8
2 | #define BIT_0 '0'
3 | #define BIT_1 '1'
--------------------------------------------------------------------------------