Skip to content

Commit 1e54109

Browse files
author
Sebastien Sikora
committed
Revised folder structure and moved demos to separate folder.
1 parent 210d843 commit 1e54109

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+2271
-335
lines changed

demos_src/1_bit_register_demo.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include "c_core.h" // Core simulator functionality
2+
#include "devices.h" // One_Bit_Register Device
3+
4+
int main () {
5+
// Verbosity flags. Set verbose & monitor_on equal to true to display verbose simulation output in the console.
6+
bool verbose = false;
7+
bool monitor_on = false;
8+
bool print_probe_samples = true;
9+
10+
// Instantiate the top-level Device (the Simulation).
11+
Simulation sim("test_sim", verbose);
12+
13+
// Add a 4-bit counter device.
14+
sim.AddComponent(new One_Bit_Register(&sim, "test_reg", monitor_on, {{"clr", true}, {"load", false}, {"d_in", false}}));
15+
16+
// Once we have added all our devices, call the simulation's Stabilise() method to finish setup.
17+
sim.Stabilise();
18+
19+
// Add a Clock and connect it to the clk input on the register.
20+
// The Clock output will be a repeating pattern of false, true, false, true, etc, starting on false on the first tick.
21+
sim.AddClock("clock_0", {false, true}, monitor_on);
22+
sim.ClockConnect("clock_0", "test_reg", "clk");
23+
24+
// Add Probes connected to the clk, load and clear inputs of the register and one to it's data output.
25+
sim.AddProbe("reg_clk_input", "test_sim:test_reg", {"clk"}, "clock_0");
26+
sim.AddProbe("reg_clr_input", "test_sim:test_reg", {"clr"}, "clock_0");
27+
sim.AddProbe("reg_load_input", "test_sim:test_reg", {"load"}, "clock_0");
28+
sim.AddProbe("reg_data_output", "test_sim:test_reg", {"d_out"}, "clock_0");
29+
30+
//sim.ChildSet("test_reg", "d_in", false);
31+
//sim.ChildSet("test_reg", "clr", true);
32+
//sim.ChildSet("test_reg", "load", false);
33+
sim.Run(3, true, verbose, false);
34+
35+
sim.ChildSet("test_reg", "d_in", true);
36+
sim.ChildSet("test_reg", "load", true);
37+
sim.ChildSet("test_reg", "clr", false);
38+
sim.Run(2, false, verbose, false);
39+
40+
sim.ChildSet("test_reg", "d_in", false);
41+
sim.ChildSet("test_reg", "load", false);
42+
sim.Run(4, false, verbose, false);
43+
44+
sim.ChildSet("test_reg", "clr", true);
45+
sim.Run(2, false, verbose, false);
46+
47+
sim.ChildSet("test_reg", "clr", false);
48+
sim.ChildSet("test_reg", "d_in", true);
49+
sim.Run(2, false, verbose, print_probe_samples);
50+
51+
return 0;
52+
}
53+

demos_src/4_bit_counter_demo.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include "c_core.h" // Core simulator functionality
2+
#include "devices.h" // Four_Bit_Counter Device
3+
4+
int main () {
5+
// Verbosity flags. Set verbose & monitor_on equal to true to display verbose simulation output in the console.
6+
bool verbose = false;
7+
bool monitor_on = false;
8+
bool print_probe_samples = true;
9+
10+
// Instantiate the top-level Device (the Simulation).
11+
Simulation* sim = new Simulation("test_sim", verbose, {false, 1});
12+
13+
// Add a 4-bit counter device.
14+
sim->AddComponent(new Four_Bit_Counter(sim, "test_counter", monitor_on, {{"run", true}}));
15+
16+
// Add a Clock and connect it to the clk input on the counter.
17+
// The Clock output will be a repeating pattern of false, true, false, true, etc, starting on false on the first tick.
18+
sim->AddClock("clock_0", {false, true}, monitor_on);
19+
sim->ClockConnect("clock_0", "test_counter", "clk");
20+
21+
// Once we have added all our devices, call the simulation's Stabilise() method to finish setup.
22+
sim->Stabilise();
23+
24+
sim->ChildMakeProbable("test_counter");
25+
26+
// Add two Probes and connect them to the counter's outputs and clk input.
27+
sim->AddProbe("clk_input", "test_sim:test_counter", {"clk"}, "clock_0");
28+
sim->AddProbe("counter_outputs", "test_sim:test_counter", {"q_0", "q_1", "q_2", "q_3"}, "clock_0");
29+
30+
//~// Run the simulation for 33 ticks.
31+
//~sim->Run(17, true, verbose, false);
32+
//~sim->ChildSet("test_counter", "run", false);
33+
//~sim->Run(2, false, verbose, false);
34+
//~sim->ChildSet("test_counter", "run", true);
35+
sim->Run(33, true, verbose, print_probe_samples);
36+
//~std::cout << static_cast<Device*>(sim->SearchForComponentPointer("test_sim:test_counter"))->GetNestingLevel() << std::endl;
37+
delete sim;
38+
39+
return 0;
40+
}
41+

demos_src/game_of_life_cell_demo.cpp

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#include <vector>
2+
#include <string>
3+
4+
#include "c_core.h" // Core simulator functionality
5+
#include "devices.h"
6+
#include "game_of_life.h"
7+
#include "utils.h"
8+
9+
int main () {
10+
// Verbosity flags. Set verbose & monitor_on equal to true to display verbose simulation output in the console.
11+
bool verbose = false;
12+
bool monitor_on = false;
13+
bool print_probe_samples = true;
14+
15+
// Instantiate the top-level Device (the Simulation).
16+
Simulation* sim = new Simulation("test_sim", verbose);
17+
18+
sim->AddComponent(new GameOfLife_Cell(sim, "test_cell", monitor_on, {{"not_clear_cycle", true}, {"not_clear_state", true}, {"not_preset_state", true}}));
19+
20+
// Once we have added all our devices, call the simulation's Stabilise() method to finish setup.
21+
sim->Stabilise();
22+
23+
// Add a Clock.
24+
// The Clock output will be a repeating pattern of false, true, false, true, etc, starting on false on the first tick.
25+
sim->AddClock("clock_0", {false, true}, monitor_on);
26+
sim->ClockConnect("clock_0", "test_cell", "clk");
27+
28+
// Add Probes.
29+
sim->AddProbe("counter_outputs", "test_sim:test_cell:counter", {"q_0", "q_1", "q_2", "q_3"}, "clock_0");
30+
sim->AddProbe("cell_siblings_out", "test_sim:test_cell", {"sibling_0_out", "sibling_0_out", "sibling_1_out", "sibling_2_out", "sibling_3_out", "sibling_4_out", "sibling_5_out", "sibling_6_out", "sibling_7_out"}, "clock_0");
31+
//~sim->AddProbe("selector_count", "test_sim:test_cell:selector:sibling_counter", {"q_0", "q_1", "q_2", "q_3"}, "clock_0");
32+
//~sim->AddProbe("selected_sibling", "test_sim:test_cell:selector", {"selected_sibling", "update_flag"}, "clock_0");
33+
//~sim->AddProbe("counter_run_in", "test_sim:test_cell:counter", {"run"}, "clock_0");
34+
35+
// Possible sibling states.
36+
std::vector<std::vector<bool>> sibling_states = {};
37+
for (int i = 0; i < 9; i ++) {
38+
std::vector<bool> states = {};
39+
for (int j = 0; j < i; j ++) {
40+
states.push_back(true);
41+
}
42+
for (int j = i; j < 8; j ++) {
43+
states.push_back(false);
44+
}
45+
sibling_states.push_back(states);
46+
}
47+
48+
// We will cycle through all the available sibling states, IE from all dead to all alive, for both cell starting states.
49+
50+
// We will start with a 'dead' starting state.
51+
for (int sibling_count_index = 0; sibling_count_index < 9; sibling_count_index ++) {
52+
for (int k = 0; k < 8; k ++) {
53+
std::cout << sibling_states[sibling_count_index][k];
54+
}
55+
std::cout << std::endl;
56+
// Make sure Cell is 'dead' and reset.
57+
sim->ChildSet("test_cell", "not_clear_cycle", false);
58+
sim->ChildSet("test_cell", "not_clear_cycle", true);
59+
sim->ChildSet("test_cell", "not_clear_state", false);
60+
sim->ChildSet("test_cell", "not_clear_state", true);
61+
// Now set sibling inputs.
62+
for (int pin_index = 0; pin_index < 8; pin_index ++) {
63+
std::string sibling_input_name = "sibling_" + std::to_string(pin_index) + "_in";
64+
sim->ChildSet("test_cell", sibling_input_name, sibling_states[sibling_count_index][pin_index]);
65+
}
66+
// Now run for 18 ticks to integrate the sibling inputs and get the new Cell state.
67+
sim->Run(18, true, verbose, print_probe_samples, false);
68+
}
69+
70+
// Now we sweep through all possible sibling states with an initial 'alive' state.
71+
for (int sibling_count_index = 0; sibling_count_index < 9; sibling_count_index ++) {
72+
for (int k = 0; k < 8; k ++) {
73+
std::cout << sibling_states[sibling_count_index][k];
74+
}
75+
std::cout << std::endl;
76+
// Make sure Cell is 'alive' and reset.
77+
sim->ChildSet("test_cell", "not_clear_cycle", false);
78+
sim->ChildSet("test_cell", "not_clear_cycle", true);
79+
sim->ChildSet("test_cell", "not_preset_state", false);
80+
sim->ChildSet("test_cell", "not_preset_state", true);
81+
// Now set sibling inputs.
82+
for (int pin_index = 0; pin_index < 8; pin_index ++) {
83+
std::string sibling_input_name = "sibling_" + std::to_string(pin_index) + "_in";
84+
sim->ChildSet("test_cell", sibling_input_name, sibling_states[sibling_count_index][pin_index]);
85+
}
86+
// Now run for 18 ticks to integrate the sibling inputs and get the new Cell state.
87+
sim->Run(18, true, verbose, print_probe_samples, false);
88+
}
89+
90+
delete sim;
91+
92+
return 0;
93+
}

demos_src/gol_cell_descision_demo.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include <iostream>
2+
3+
#include "c_core.h" // Core simulator functionality
4+
#include "devices.h"
5+
#include "game_of_life.h"
6+
#include "utils.h"
7+
8+
int main () {
9+
// Verbosity flags. Set verbose & monitor_on equal to true to display verbose simulation output in the console.
10+
bool verbose = false;
11+
bool monitor_on = false;
12+
bool print_probe_samples = true;
13+
14+
// Instantiate the top-level Device (the Simulation).
15+
Simulation* sim = new Simulation("test_sim", verbose);
16+
17+
sim->AddComponent(new GameOfLife_Cell_Decider(sim, "test_decider", monitor_on));
18+
19+
// Once we have added all our devices, call the simulation's Stabilise() method to finish setup.
20+
sim->Stabilise();
21+
22+
// Add a Clock.
23+
// The Clock output will be a repeating pattern of false, true, false, true, etc, starting on false on the first tick.
24+
sim->AddClock("clock_0", {false, true}, monitor_on);
25+
26+
// Add Probes.
27+
sim->AddProbe("decider_alive_in", "test_sim:test_decider", {"alive_in"}, "clock_0");
28+
sim->AddProbe("decider_count_in", "test_sim:test_decider", {"count_in_0", "count_in_1", "count_in_2"}, "clock_0");
29+
sim->AddProbe("decider_alive_out", "test_sim:test_decider", {"alive_out"}, "clock_0");
30+
31+
std::vector<bool> prior_state = {false, true};
32+
for (const auto& this_state : prior_state) {
33+
sim->ChildSet("test_decider", "alive_in", this_state);
34+
for (int value = 0; value < 8; value ++) {
35+
std::vector<bool> counter_outputs = IntToStates(value, 3);
36+
for (int output_index = 0; output_index < 3; output_index ++) {
37+
std::string count_pin_name = "count_in_" + std::to_string(output_index);
38+
sim->ChildSet("test_decider", count_pin_name, counter_outputs[output_index]);
39+
}
40+
if ((value == 0) && (!this_state)) {
41+
sim->Run(1, true, verbose, false);
42+
} else if ((value == 7) && (this_state)) {
43+
sim->Run(1, false, verbose, print_probe_samples);
44+
} else {
45+
sim->Run(1, false, verbose, false);
46+
}
47+
}
48+
}
49+
50+
delete sim;
51+
52+
return 0;
53+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include "c_core.h" // Core simulator functionality
2+
#include "devices.h"
3+
#include "game_of_life.h"
4+
#include "utils.h"
5+
6+
int main () {
7+
// Verbosity flags. Set verbose & monitor_on equal to true to display verbose simulation output in the console.
8+
bool verbose = false;
9+
bool monitor_on = false;
10+
bool print_probe_samples = true;
11+
12+
// Instantiate the top-level Device (the Simulation).
13+
Simulation* sim = new Simulation("test_sim", verbose);
14+
15+
sim->AddComponent(new GameOfLife_Cell_SiblingSelector(sim, "test_selector", monitor_on, {{"not_clear", true}}));
16+
17+
// Once we have added all our devices, call the simulation's Stabilise() method to finish setup.
18+
sim->Stabilise();
19+
20+
// Add a Clock.
21+
// The Clock output will be a repeating pattern of false, true, false, true, etc, starting on false on the first tick.
22+
sim->AddClock("clock_0", {false, true}, monitor_on);
23+
sim->ClockConnect("clock_0", "test_selector", "clk");
24+
25+
// Add Probes.
26+
sim->AddProbe("siblings_in", "test_sim:test_selector", {"sibling_0", "sibling_1", "sibling_2", "sibling_3", "sibling_4", "sibling_5", "sibling_6", "sibling_7"}, "clock_0");
27+
sim->AddProbe("selected_sibling", "test_sim:test_selector", {"selected_sibling"}, "clock_0");
28+
sim->AddProbe("update_flag", "test_sim:test_selector", {"update_flag"}, "clock_0");
29+
30+
sim->ChildSet("test_selector", "sibling_0", true);
31+
sim->ChildSet("test_selector", "sibling_1", false);
32+
sim->ChildSet("test_selector", "sibling_2", true);
33+
sim->ChildSet("test_selector", "sibling_3", false);
34+
sim->ChildSet("test_selector", "sibling_4", true);
35+
sim->ChildSet("test_selector", "sibling_5", false);
36+
sim->ChildSet("test_selector", "sibling_6", true);
37+
sim->ChildSet("test_selector", "sibling_7", false);
38+
39+
sim->ChildSet("test_selector", "not_clear", false);
40+
sim->ChildSet("test_selector", "not_clear", true);
41+
42+
sim->Run(20, true, verbose, print_probe_samples);
43+
44+
delete sim;
45+
46+
return 0;
47+
}

demos_src/jk_ff_aspc_demo.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include "c_core.h" // Core simulator functionality
2+
#include "devices.h" // JK flip-flop Device
3+
4+
int main () {
5+
// Verbosity flags. Set verbose & monitor_on equal to true to display verbose simulation output in the console.
6+
bool verbose = false;
7+
bool monitor_on = false;
8+
bool print_probe_samples = true;
9+
10+
// Instantiate the top-level Device (the Simulation).
11+
Simulation sim("test_sim", verbose);
12+
13+
// Add a jk flip-flop Device.
14+
sim.AddComponent(new JK_FF_ASPC(&sim, "test_ff", monitor_on, {{"j", false}, {"k", false}, {"not_p", true}, {"not_c", true}}));
15+
16+
// Once we have added all our devices, call the simulation's Stabilise() method to finish setup.
17+
sim.Stabilise();
18+
19+
// Add a Clock and connect it to the clk input on the jk flip-flop.
20+
// The Clock output will be a repeating pattern of false, true, false, true, etc, starting on false on the first tick.
21+
sim.AddClock("clock_0", {false, true}, monitor_on);
22+
sim.ClockConnect("clock_0", "test_ff", "clk");
23+
24+
// Add two Probes and connect them to the jk flip-flop's outputs and clk input.
25+
sim.AddProbe("flip-flop outputs", "test_sim:test_ff", {"q", "not_q"}, "clock_0");
26+
sim.AddProbe("flip-flop clk input", "test_sim:test_ff", {"clk"}, "clock_0");
27+
28+
sim.Run(1, true, verbose, false);
29+
30+
sim.ChildSet("test_ff", "not_c", false);
31+
sim.ChildSet("test_ff", "not_c", true);
32+
sim.Run(1, false, verbose, false);
33+
34+
sim.ChildSet("test_ff", "not_p", false);
35+
sim.ChildSet("test_ff", "not_p", true);
36+
sim.Run(1, false, verbose, false);
37+
// Run the simulation for ten ticks.
38+
39+
sim.ChildSet("test_ff", "j", true);
40+
sim.ChildSet("test_ff", "k", true);
41+
sim.Run(10, false, verbose, print_probe_samples);
42+
43+
return 0;
44+
}

demos_src/jk_ff_demo.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include "c_core.h" // Core simulator functionality
2+
#include "devices.h" // JK flip-flop Device
3+
4+
int main () {
5+
// Verbosity flags. Set verbose & monitor_on equal to true to display verbose simulation output in the console.
6+
bool verbose = true;
7+
bool monitor_on = false;
8+
bool print_probe_samples = true;
9+
10+
// Instantiate the top-level Device (the Simulation).
11+
Simulation sim("test_sim", verbose);
12+
13+
// Add a jk flip-flop Device.
14+
sim.AddComponent(new JK_FF(&sim, "test_ff", monitor_on, {{"j", true}, {"k", true}}));
15+
16+
// We could leave out the default in pin state vector argument, and instead connect 'j' and 'k' in pins
17+
// to the Simulation's 'true' hidden in pin...
18+
//~sim.Connect("true", "test_ff", "j");
19+
//~sim.Connect("true", "test_ff", "k");
20+
// ...or we could set the pin states manually.
21+
//~sim.ChildSet("test_ff", "j", true);
22+
//~sim.ChildSet("test_ff", "k", true);
23+
24+
// Once we have added all our devices, call the simulation's Stabilise() method to finish setup.
25+
sim.Stabilise();
26+
27+
//~// Add a Clock and connect it to the clk input on the jk flip-flop.
28+
//~// The Clock output will be a repeating pattern of false, true, false, true, etc, starting on false on the first tick.
29+
sim.AddClock("clock_0", {false, true}, monitor_on);
30+
sim.ClockConnect("clock_0", "test_ff", "clk");
31+
32+
//~// Add two Probes and connect them to the jk flip-flop's outputs and clk input.
33+
//~// Note - we need to use the target Component's 'full name' to connect a Probe.
34+
sim.AddProbe("flip-flop outputs", "test_sim:test_ff", {"q", "not_q"}, "clock_0");
35+
sim.AddProbe("flip-flop clk input", "test_sim:test_ff", {"clk"}, "clock_0");
36+
37+
//~// Run the simulation for ten ticks.
38+
sim.Run(3, true, verbose, print_probe_samples);
39+
40+
return 0;
41+
}

0 commit comments

Comments
 (0)