Hi,
Im in the process of writing a simple digital logic circuit simulator
application (similar to B2Logic). What would be the best approach or
design for a project like that? I mean should I use the object-oriented
design: making gates as classes, and if so, should I make ALL gates as
classes or should it be just the BASIC gates (AND, NOT, OR) and build
all other components (XOR, NOR, NAND, FULLADDER, etc.) as C-style
FUNCTIONS that use those basic classes?
I guess my questions should be: Whats the most common design in
commercial applications (e.g. B2Logic)? Is it pure, C-Style code, or is
it OOD? And how is it generally done (details are highly
appreciated)???
Pete, each logic device should be a class if it can have internal state data
(remember object = data + functions). By having say a "D FlipFlop" class,
you can then construct many flip flops and each will remember its state (ON
or OFF) and any other settings you might want. Even a simple gate might
have settings such as delay which might be settable differently for each
gate. The magic of OOP is that data and actions on that data are
represented by the class. Without OOP you have to store that data in
variables which you have to keep track of - and that leads to poor quality,
hard to maintain code - exactly why OOP was invented. So I would make my
logic devices classes without a second thought.
A different issue is whether those logic device classes should all inherit
from a common ancestor. Inheritance makes sense when the objects share
their "functional essence". This can be hard to pick, but a good indicator
is that some of the internal guts are in the ancestor and are used by the
desendants. Another indicator is that true polymorhphism exists - i.e.
virtual functions are actually useful.
If I had the job, I would do like this:
- a "device" class for each logic device type all inheriting from a common
ancestor. The ancestor offers functions which allow discovery of the input
and output terminals, and a function which lets you define the states of the
inputs.
- a "circuit" class which contains (an array of) as many "device" objects as
I want to add, and a representation of the interconnections betweeen all the
objects.
- a "stepper" class which drives the circuit class, transferring logic
outputs to inputs at time steps.
- a "capture" class which looks at the circuit object and records the state
of desired points and logs or outputs these to screen.
I write OO code year in year out for a living with less electronics these
days, and would never dream of using C for anything except an embedded micro
or device driver, because it has no "big ideas". C++ is a nasty beast, but
brings a massive boost to 1. Productivity 2. Code quality 3. Size of
project a single programmer can manage without self destructing. Despite
its problems, C++ has the true 00 ability to let you map real world things
and concepts onto objects - letting you work on a higher plane.
Roger Lascelles