wiRedPanda
Logic Circuit Simulator
Loading...
Searching...
No Matches
DolphinEdits.cpp
Go to the documentation of this file.
1// Copyright 2015 - 2026, GIBIS-UNIFESP and the wiRedPanda contributors
2// SPDX-License-Identifier: GPL-3.0-or-later
3
5
6#include <algorithm>
7
9
10namespace DolphinEdits {
11
12void applyToCells(SignalModel &model, const QModelIndexList &cells, const std::function<int(int)> &valueFn)
13{
14 SignalModel::BulkEditGuard guard(model);
15 for (const auto &cell : cells) {
16 model.setValue(cell.row(), cell.column(), valueFn(model.value(cell.row(), cell.column())));
17 }
18}
19
20void clockWave(SignalModel &model, const QModelIndexList &cells, const int firstCol, const int period)
21{
22 // First half of each period is LOW, second half is HIGH (50% duty cycle), with the
23 // phase anchored at firstCol so the waveform starts at 0 regardless of the selection.
24 const int halfPeriod = period / 2;
25
26 SignalModel::BulkEditGuard guard(model);
27 for (const auto &cell : cells) {
28 const int value = ((cell.column() - firstCol) % period < halfPeriod ? 0 : 1);
29 model.setValue(cell.row(), cell.column(), value);
30 }
31}
32
33void combinational(SignalModel &model, const int inputPorts, const int columns)
34{
35 // Gray-code-like input patterns: row 0 toggles every 1 column (period=2), row 1 every
36 // 2 columns (period=4), etc. Together they enumerate all input combinations.
37 int halfClockPeriod = 1;
38 int clockPeriod = 2;
39
40 SignalModel::BulkEditGuard guard(model);
41 for (int row = 0; row < inputPorts; ++row) {
42 for (int col = 0; col < columns; ++col) {
43 model.setValue(row, col, (col % clockPeriod < halfClockPeriod ? 0 : 1));
44 }
45
46 // Double the period for each successive input bit; cap at max int-safe values
47 halfClockPeriod = (std::min)(clockPeriod, 524288);
48 clockPeriod = (std::min)(2 * clockPeriod, 1048576);
49 }
50}
51
52void clearInputs(SignalModel &model, const int inputPorts)
53{
54 SignalModel::BulkEditGuard guard(model);
55 for (int row = 0; row < inputPorts; ++row) {
56 for (int col = 0; col < model.columnCount(); ++col) {
57 model.setValue(row, col, 0);
58 }
59 }
60}
61
62int lastNonZeroColumn(const SignalModel &model, const int inputPorts)
63{
64 for (int col = model.columnCount() - 1; col >= 0; --col) {
65 for (int row = 0; row < inputPorts; ++row) {
66 if (model.value(row, col) != 0) {
67 return col;
68 }
69 }
70 }
71
72 return 0;
73}
74
75void growInputColumns(SignalModel &model, const int inputPorts, const int oldLength, const int newLength)
76{
77 // New input columns must be explicitly filled with zeros; output columns are populated
78 // by the simulation run and don't need pre-filling.
79 SignalModel::BulkEditGuard guard(model);
80 for (int row = 0; row < inputPorts; ++row) {
81 for (int col = oldLength; col < newLength; ++col) {
82 model.setValue(row, col, 0);
83 }
84 }
85}
86
87} // namespace DolphinEdits
DolphinEdits: pure value-grid mutations for the beWavedDolphin waveform table.
SignalModel for the beWavedDolphin waveform table.
QStandardItemModel subclass that makes all cells non-editable.
Definition SignalModel.h:21
void setValue(int row, int col, int value)
Sets the logic value (0/1) of the cell at (row, col).
int value(int row, int col) const
Returns the logic value (0/1) of the cell at (row, col).
Pure mutations of a SignalModel's value grid — the data side of the editor's commands (set/invert/cle...
void combinational(SignalModel &model, const int inputPorts, const int columns)
void clearInputs(SignalModel &model, const int inputPorts)
Sets every input cell (the first inputPorts rows, over the model's columns) to 0.
void applyToCells(SignalModel &model, const QModelIndexList &cells, const std::function< int(int)> &valueFn)
Sets each cell in cells to valueFn(currentValue).
void growInputColumns(SignalModel &model, const int inputPorts, const int oldLength, const int newLength)
int lastNonZeroColumn(const SignalModel &model, const int inputPorts)
Returns the index of the last column with any non-zero input value, or 0 if none.
void clockWave(SignalModel &model, const QModelIndexList &cells, const int firstCol, const int period)