ZLayout EDA Library v1.0.0
Advanced Electronic Design Automation Layout Library with Bilingual Documentation
Loading...
Searching...
No Matches
digital_components.hpp
Go to the documentation of this file.
1
8
9#pragma once
10
12#include <memory>
13#include <string>
14#include <vector>
15#include <functional>
16#include <map>
17
18namespace zlayout {
19namespace components {
20
24enum class LogicFamily {
25 TTL, // Transistor-Transistor Logic
26 CMOS, // Complementary Metal-Oxide-Semiconductor
27 ECL, // Emitter-Coupled Logic
28 LVTTL, // Low-Voltage TTL
29 LVCMOS, // Low-Voltage CMOS
30 LVDS, // Low-Voltage Differential Signaling
31 Custom // Custom logic levels
32};
33
38public:
39 DigitalComponent(const std::string& name, ComponentCategory category);
40
41 // Logic family
43 void setLogicFamily(LogicFamily family) { logic_family_ = family; }
44
45 // Logic levels
46 double getVil() const { return vil_; } // Input Low Voltage
47 void setVil(double vil) { vil_ = vil; }
48
49 double getVih() const { return vih_; } // Input High Voltage
50 void setVih(double vih) { vih_ = vih; }
51
52 double getVol() const { return vol_; } // Output Low Voltage
53 void setVol(double vol) { vol_ = vol; }
54
55 double getVoh() const { return voh_; } // Output High Voltage
56 void setVoh(double voh) { voh_ = voh; }
57
58 // Signal evaluation
59 virtual void evaluateLogic() = 0;
60 virtual void propagateSignals();
61
62 // Simulation
63 void simulate(double time_step) override;
64
65protected:
67 double vil_ = 0.8; // V
68 double vih_ = 2.0; // V
69 double vol_ = 0.4; // V
70 double voh_ = 2.4; // V
71
72 // Helper functions
73 SignalState voltageToSignalState(double voltage) const;
74 double signalStateToVoltage(SignalState state) const;
75};
76
81public:
82 enum class GateType {
84 };
85
86 LogicGate(const std::string& name, GateType type, size_t input_count = 2);
87
88 GateType getGateType() const { return gate_type_; }
89 size_t getInputCount() const { return input_count_; }
90
91 // Logic evaluation
92 void evaluateLogic() override;
93 std::string getDescription() const override;
94
95private:
96 GateType gate_type_;
97 size_t input_count_;
98
99 // Gate-specific logic functions
100 SignalState evaluateAnd(const std::vector<SignalState>& inputs) const;
101 SignalState evaluateOr(const std::vector<SignalState>& inputs) const;
102 SignalState evaluateNot(SignalState input) const;
103 SignalState evaluateNand(const std::vector<SignalState>& inputs) const;
104 SignalState evaluateNor(const std::vector<SignalState>& inputs) const;
105 SignalState evaluateXor(const std::vector<SignalState>& inputs) const;
106 SignalState evaluateXnor(const std::vector<SignalState>& inputs) const;
107};
108
113public:
114 DFlipFlop(const std::string& name, bool has_reset = true, bool has_set = false);
115
116 // State
117 SignalState getQ() const { return q_; }
118 SignalState getQbar() const { return qbar_; }
119
120 // Control
121 bool hasReset() const { return has_reset_; }
122 bool hasSet() const { return has_set_; }
123
124 // Clock edge detection
125 enum class EdgeType { Rising, Falling, Both };
126 EdgeType getEdgeType() const { return edge_type_; }
127 void setEdgeType(EdgeType type) { edge_type_ = type; }
128
129 // Logic evaluation
130 void evaluateLogic() override;
131 void reset() override;
132 std::string getDescription() const override;
133
134private:
137 SignalState last_clock_ = SignalState::Low;
138 bool has_reset_;
139 bool has_set_;
140 EdgeType edge_type_ = EdgeType::Rising;
141
142 bool isClockEdge(SignalState current_clock) const;
143};
144
149public:
150 JKFlipFlop(const std::string& name, bool has_reset = true, bool has_set = false);
151
152 // State
153 SignalState getQ() const { return q_; }
154 SignalState getQbar() const { return qbar_; }
155
156 // Control
157 bool hasReset() const { return has_reset_; }
158 bool hasSet() const { return has_set_; }
159
160 // Logic evaluation
161 void evaluateLogic() override;
162 void reset() override;
163 std::string getDescription() const override;
164
165private:
168 SignalState last_clock_ = SignalState::Low;
169 bool has_reset_;
170 bool has_set_;
171};
172
176class SRLatch : public DigitalComponent {
177public:
178 SRLatch(const std::string& name, bool is_gated = false);
179
180 // State
181 SignalState getQ() const { return q_; }
182 SignalState getQbar() const { return qbar_; }
183
184 bool isGated() const { return is_gated_; }
185
186 // Logic evaluation
187 void evaluateLogic() override;
188 void reset() override;
189 std::string getDescription() const override;
190
191private:
194 bool is_gated_;
195};
196
201public:
202 BinaryCounter(const std::string& name, size_t width, bool is_up_counter = true);
203
204 // Counter properties
205 size_t getWidth() const { return width_; }
206 bool isUpCounter() const { return is_up_counter_; }
207
208 // Count value
209 uint64_t getCount() const { return count_; }
210 void setCount(uint64_t value);
211
212 uint64_t getMaxCount() const { return max_count_; }
213
214 // Control
215 void enableCount(bool enable) { count_enable_ = enable; }
216 bool isCountEnabled() const { return count_enable_; }
217
218 // Logic evaluation
219 void evaluateLogic() override;
220 void reset() override;
221 std::string getDescription() const override;
222
223private:
224 size_t width_;
225 bool is_up_counter_;
226 uint64_t count_ = 0;
227 uint64_t max_count_;
228 bool count_enable_ = true;
229 SignalState last_clock_ = SignalState::Low;
230
231 void updateOutputs();
232};
233
238public:
239 enum class ShiftType {
240 SISO, // Serial In, Serial Out
241 SIPO, // Serial In, Parallel Out
242 PISO, // Parallel In, Serial Out
243 PIPO // Parallel In, Parallel Out
244 };
245
246 ShiftRegister(const std::string& name, size_t width, ShiftType type);
247
248 // Properties
249 size_t getWidth() const { return width_; }
250 ShiftType getShiftType() const { return shift_type_; }
251
252 // Direction
253 enum class Direction { Left, Right };
254 Direction getDirection() const { return direction_; }
255 void setDirection(Direction dir) { direction_ = dir; }
256
257 // Data
258 const std::vector<SignalState>& getData() const { return data_; }
259 void setData(const std::vector<SignalState>& data);
260
261 // Logic evaluation
262 void evaluateLogic() override;
263 void reset() override;
264 std::string getDescription() const override;
265
266private:
267 size_t width_;
268 ShiftType shift_type_;
269 Direction direction_ = Direction::Right;
270 std::vector<SignalState> data_;
271 SignalState last_clock_ = SignalState::Low;
272
273 void shiftData();
274 void updateOutputs();
275};
276
281public:
282 Multiplexer(const std::string& name, size_t input_count);
283
284 // Properties
285 size_t getInputCount() const { return input_count_; }
286 size_t getSelectWidth() const { return select_width_; }
287
288 // Current selection
289 size_t getSelectedInput() const { return selected_input_; }
290
291 // Logic evaluation
292 void evaluateLogic() override;
293 std::string getDescription() const override;
294
295private:
296 size_t input_count_;
297 size_t select_width_;
298 size_t selected_input_ = 0;
299
300 size_t calculateSelectValue() const;
301};
302
307public:
308 Demultiplexer(const std::string& name, size_t output_count);
309
310 // Properties
311 size_t getOutputCount() const { return output_count_; }
312 size_t getSelectWidth() const { return select_width_; }
313
314 // Current selection
315 size_t getSelectedOutput() const { return selected_output_; }
316
317 // Logic evaluation
318 void evaluateLogic() override;
319 std::string getDescription() const override;
320
321private:
322 size_t output_count_;
323 size_t select_width_;
324 size_t selected_output_ = 0;
325
326 size_t calculateSelectValue() const;
327};
328
332class Decoder : public DigitalComponent {
333public:
334 Decoder(const std::string& name, size_t input_width, bool has_enable = true);
335
336 // Properties
337 size_t getInputWidth() const { return input_width_; }
338 size_t getOutputCount() const { return output_count_; }
339 bool hasEnable() const { return has_enable_; }
340
341 // Logic evaluation
342 void evaluateLogic() override;
343 std::string getDescription() const override;
344
345private:
346 size_t input_width_;
347 size_t output_count_;
348 bool has_enable_;
349
350 size_t calculateInputValue() const;
351};
352
356class Encoder : public DigitalComponent {
357public:
358 Encoder(const std::string& name, size_t input_count, bool is_priority = false);
359
360 // Properties
361 size_t getInputCount() const { return input_count_; }
362 size_t getOutputWidth() const { return output_width_; }
363 bool isPriority() const { return is_priority_; }
364
365 // Logic evaluation
366 void evaluateLogic() override;
367 std::string getDescription() const override;
368
369private:
370 size_t input_count_;
371 size_t output_width_;
372 bool is_priority_;
373
374 size_t findActiveInput() const;
375 size_t findHighestPriorityInput() const;
376};
377
382public:
383 FullAdder(const std::string& name);
384
385 // Logic evaluation
386 void evaluateLogic() override;
387 std::string getDescription() const override;
388};
389
394public:
395 RippleCarryAdder(const std::string& name, size_t width);
396
397 // Properties
398 size_t getWidth() const { return width_; }
399
400 // Values
401 uint64_t getA() const { return a_; }
402 uint64_t getB() const { return b_; }
403 uint64_t getSum() const { return sum_; }
404 bool getCarryOut() const { return carry_out_; }
405
406 // Logic evaluation
407 void evaluateLogic() override;
408 std::string getDescription() const override;
409
410private:
411 size_t width_;
412 uint64_t a_ = 0;
413 uint64_t b_ = 0;
414 uint64_t sum_ = 0;
415 bool carry_out_ = false;
416
417 void calculateInputValues();
418 void updateOutputs();
419};
420
424class Memory : public DigitalComponent {
425public:
426 enum class MemoryType {
427 RAM, // Random Access Memory
428 ROM, // Read-Only Memory
429 EEPROM, // Electrically Erasable Programmable ROM
430 Flash // Flash Memory
431 };
432
433 Memory(const std::string& name, size_t address_width, size_t data_width, MemoryType type);
434
435 // Properties
436 size_t getAddressWidth() const { return address_width_; }
437 size_t getDataWidth() const { return data_width_; }
438 size_t getMemorySize() const { return memory_size_; }
439 MemoryType getMemoryType() const { return memory_type_; }
440
441 // Memory operations
442 uint64_t readData(uint64_t address) const;
443 void writeData(uint64_t address, uint64_t data);
444
445 // Initialization
446 void loadFromFile(const std::string& filename);
447 void saveToFile(const std::string& filename) const;
448
449 // Logic evaluation
450 void evaluateLogic() override;
451 std::string getDescription() const override;
452
453private:
454 size_t address_width_;
455 size_t data_width_;
456 size_t memory_size_;
457 MemoryType memory_type_;
458 std::vector<uint64_t> memory_data_;
459
460 uint64_t current_address_ = 0;
461 uint64_t current_data_ = 0;
462 SignalState last_clock_ = SignalState::Low;
463 SignalState last_write_enable_ = SignalState::Low;
464
465 void updateCurrentAddress();
466 void updateCurrentData();
467 void updateOutputs();
468};
469
474public:
475 // Basic gates
476 static std::shared_ptr<LogicGate> createLogicGate(
477 const std::string& name, LogicGate::GateType type, size_t input_count = 2);
478
479 // Flip-flops and latches
480 static std::shared_ptr<DFlipFlop> createDFlipFlop(
481 const std::string& name, bool has_reset = true, bool has_set = false);
482
483 static std::shared_ptr<JKFlipFlop> createJKFlipFlop(
484 const std::string& name, bool has_reset = true, bool has_set = false);
485
486 static std::shared_ptr<SRLatch> createSRLatch(
487 const std::string& name, bool is_gated = false);
488
489 // Counters and registers
490 static std::shared_ptr<BinaryCounter> createBinaryCounter(
491 const std::string& name, size_t width, bool is_up_counter = true);
492
493 static std::shared_ptr<ShiftRegister> createShiftRegister(
494 const std::string& name, size_t width, ShiftRegister::ShiftType type);
495
496 // Combinational logic
497 static std::shared_ptr<Multiplexer> createMultiplexer(
498 const std::string& name, size_t input_count);
499
500 static std::shared_ptr<Demultiplexer> createDemultiplexer(
501 const std::string& name, size_t output_count);
502
503 static std::shared_ptr<Decoder> createDecoder(
504 const std::string& name, size_t input_width, bool has_enable = true);
505
506 static std::shared_ptr<Encoder> createEncoder(
507 const std::string& name, size_t input_count, bool is_priority = false);
508
509 // Arithmetic
510 static std::shared_ptr<FullAdder> createFullAdder(const std::string& name);
511
512 static std::shared_ptr<RippleCarryAdder> createRippleCarryAdder(
513 const std::string& name, size_t width);
514
515 // Memory
516 static std::shared_ptr<Memory> createMemory(
517 const std::string& name, size_t address_width, size_t data_width,
518 Memory::MemoryType type);
519
520 // Standard digital library components
522
523 // Common digital building blocks
524 static std::shared_ptr<Component> create74Series(
525 const std::string& part_number, const std::string& name);
526
527 static std::shared_ptr<Component> create4000Series(
528 const std::string& part_number, const std::string& name);
529};
530
531} // namespace components
532} // namespace zlayout
std::string getDescription() const override
BinaryCounter(const std::string &name, size_t width, bool is_up_counter=true)
Component(const std::string &name, ComponentCategory category)
Component library for managing component definitions.
std::string getDescription() const override
DFlipFlop(const std::string &name, bool has_reset=true, bool has_set=false)
Decoder(const std::string &name, size_t input_width, bool has_enable=true)
void evaluateLogic() override
std::string getDescription() const override
Demultiplexer(const std::string &name, size_t output_count)
std::string getDescription() const override
static std::shared_ptr< ShiftRegister > createShiftRegister(const std::string &name, size_t width, ShiftRegister::ShiftType type)
static void registerStandardComponents(ComponentLibrary &library)
static std::shared_ptr< Demultiplexer > createDemultiplexer(const std::string &name, size_t output_count)
static std::shared_ptr< FullAdder > createFullAdder(const std::string &name)
static std::shared_ptr< DFlipFlop > createDFlipFlop(const std::string &name, bool has_reset=true, bool has_set=false)
static std::shared_ptr< Component > create4000Series(const std::string &part_number, const std::string &name)
static std::shared_ptr< RippleCarryAdder > createRippleCarryAdder(const std::string &name, size_t width)
static std::shared_ptr< BinaryCounter > createBinaryCounter(const std::string &name, size_t width, bool is_up_counter=true)
static std::shared_ptr< SRLatch > createSRLatch(const std::string &name, bool is_gated=false)
static std::shared_ptr< Encoder > createEncoder(const std::string &name, size_t input_count, bool is_priority=false)
static std::shared_ptr< Multiplexer > createMultiplexer(const std::string &name, size_t input_count)
static std::shared_ptr< LogicGate > createLogicGate(const std::string &name, LogicGate::GateType type, size_t input_count=2)
static std::shared_ptr< JKFlipFlop > createJKFlipFlop(const std::string &name, bool has_reset=true, bool has_set=false)
static std::shared_ptr< Memory > createMemory(const std::string &name, size_t address_width, size_t data_width, Memory::MemoryType type)
static std::shared_ptr< Decoder > createDecoder(const std::string &name, size_t input_width, bool has_enable=true)
static std::shared_ptr< Component > create74Series(const std::string &part_number, const std::string &name)
SignalState voltageToSignalState(double voltage) const
double signalStateToVoltage(SignalState state) const
DigitalComponent(const std::string &name, ComponentCategory category)
void simulate(double time_step) override
std::string getDescription() const override
void evaluateLogic() override
Encoder(const std::string &name, size_t input_count, bool is_priority=false)
FullAdder(const std::string &name)
std::string getDescription() const override
std::string getDescription() const override
JKFlipFlop(const std::string &name, bool has_reset=true, bool has_set=false)
LogicGate(const std::string &name, GateType type, size_t input_count=2)
std::string getDescription() const override
uint64_t readData(uint64_t address) const
Memory(const std::string &name, size_t address_width, size_t data_width, MemoryType type)
void loadFromFile(const std::string &filename)
void evaluateLogic() override
void saveToFile(const std::string &filename) const
void writeData(uint64_t address, uint64_t data)
std::string getDescription() const override
std::string getDescription() const override
Multiplexer(const std::string &name, size_t input_count)
std::string getDescription() const override
RippleCarryAdder(const std::string &name, size_t width)
void evaluateLogic() override
SRLatch(const std::string &name, bool is_gated=false)
std::string getDescription() const override
std::string getDescription() const override
const std::vector< SignalState > & getData() const
void setData(const std::vector< SignalState > &data)
ShiftRegister(const std::string &name, size_t width, ShiftType type)
Hierarchical EDA Component System.
SignalState
Signal states in digital circuits.
Definition component.hpp:46
ComponentCategory
Component categories for organization.
Definition component.hpp:56
LogicFamily
Logic family types.
Main namespace for ZLayout library.
Definition component.hpp:20