Skip to content

Commit 4d8da6a

Browse files
author
Richard Grafton
authored
Merge pull request TeachingTechnologistBeth#8 from jackoson/master
set Or gate to actual size
2 parents 39ef009 + 9ca6448 commit 4d8da6a

5 files changed

Lines changed: 200 additions & 5 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,4 @@ $RECYCLE.BIN/
6666

6767
# Windows shortcuts
6868
*.lnk
69+
/bin/

src/com/modsim/gui/view/ContextMenu.java

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.modsim.gui.MemEdit;
1212
import com.modsim.modules.BaseModule;
1313
import com.modsim.modules.BaseModule.AvailableModules;
14+
import com.modsim.modules.LEDMatrix;
1415
import com.modsim.modules.NRAM;
1516
import com.modsim.modules.Register;
1617
import com.modsim.modules.parts.Port;
@@ -27,7 +28,7 @@ public class ContextMenu {
2728
private Port port;
2829

2930
private JMenuItem rmLink, rotCW, rotCCW, rot180, copy, paste, delete,
30-
ramEdit, ramClear, regEdit, regClear, labelEdit, labelSize;
31+
ramEdit, ramClear, regEdit, regClear, labelEdit, labelSize, persistanceOn, persistanceOff;
3132

3233
/**
3334
* Instantiates the menu system, generating the menu items
@@ -161,6 +162,37 @@ public void actionPerformed(ActionEvent e) {
161162
}
162163
}
163164
});
165+
166+
167+
///////////////LED Matrix specific
168+
169+
//Toggle Persistance
170+
persistanceOff = new JMenuItem("Turn off persist");
171+
persistanceOff.addActionListener(new ActionListener(){
172+
public void actionPerformed(ActionEvent e) {
173+
for (PickableEntity entity : entities) {
174+
if (entity.getType() == PickableEntity.MODULE &&
175+
((BaseModule)entity).getModType().equals(AvailableModules.LEDMatrix)) {
176+
LEDMatrix ledmatrix = (LEDMatrix) entity;
177+
ledmatrix.turnOffPersist();
178+
}
179+
}
180+
}
181+
});
182+
183+
persistanceOn = new JMenuItem("Turn on persist");
184+
persistanceOn.addActionListener(new ActionListener(){
185+
public void actionPerformed(ActionEvent e) {
186+
for (PickableEntity entity : entities) {
187+
if (entity.getType() == PickableEntity.MODULE &&
188+
((BaseModule)entity).getModType().equals(AvailableModules.LEDMatrix)) {
189+
LEDMatrix ledmatrix = (LEDMatrix) entity;
190+
ledmatrix.turnOnPersist();
191+
}
192+
}
193+
}
194+
});
195+
164196

165197
}
166198

@@ -202,7 +234,19 @@ public void showEntityMenu(List<PickableEntity> modules, int x, int y) {
202234
menu.addSeparator();
203235
menu.add(ramEdit);
204236
menu.add(ramClear);
205-
237+
break;
238+
}
239+
}
240+
241+
for (PickableEntity e : entities) {
242+
// If it's an LEDMatrix module
243+
if (e.getType() == PickableEntity.MODULE && ((BaseModule)e).getModType().equals(AvailableModules.LEDMatrix)) {
244+
menu.addSeparator();
245+
if(((LEDMatrix)e).isPersistEnabled()){
246+
menu.add(persistanceOff);
247+
}else{
248+
menu.add(persistanceOn);
249+
}
206250
break;
207251
}
208252
}

src/com/modsim/modules/BaseModule.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,8 @@ public enum AvailableModules {
712712
LEFT_SHIFT(new Shift(true), "Left-shift"),
713713
RIGHT_SHIFT(new Shift(false), "Right-shift"),
714714
SPLIT_MERGE(new SplitMerge(), "Splitter / Merger"),
715-
SWITCH(new SwitchInput(), "Switch Input");
715+
SWITCH(new SwitchInput(), "Switch Input"),
716+
LEDMatrix(new LEDMatrix(), "16x16 LED matrix");
716717

717718
/**
718719
* The module represented by this enum value, to use to instantiate and display in GUI.
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
package com.modsim.modules;
2+
3+
import java.awt.Graphics2D;
4+
import java.util.ArrayList;
5+
import java.util.Arrays;
6+
import java.util.Collections;
7+
import java.util.HashMap;
8+
import java.util.List;
9+
10+
import com.modsim.modules.parts.LEDRow;
11+
import com.modsim.modules.parts.Port;
12+
import com.modsim.modules.ports.Input;
13+
import com.modsim.res.Colors;
14+
import com.modsim.simulator.PickableEntity;
15+
import com.modsim.util.BinData;
16+
import com.modsim.util.HexReader;
17+
import com.modsim.util.HexWriter;
18+
19+
public class LEDMatrix extends BaseModule {
20+
21+
private final List<Input> dIn;
22+
private final Input contIn;
23+
24+
private final List<List<LEDRow>> matrix;
25+
private int previousRow = 0;
26+
private boolean persist = false;
27+
28+
LEDMatrix(){
29+
w = 150;
30+
h = 150;
31+
32+
dIn = Collections.unmodifiableList(Arrays.asList(new Input[]{
33+
addInput("Input A", -50, Port.DATA),
34+
addInput("Input B", -25, Port.DATA),
35+
addInput("Input C", 25, Port.DATA),
36+
addInput("Input D", 50, Port.DATA),
37+
}));
38+
contIn = addInput("Line Select", 0, Port.CTRL);
39+
40+
int y=35;
41+
// Add display
42+
matrix = new ArrayList<List<LEDRow>>();
43+
for(int i=0; i<16; i++){
44+
int x=-46;
45+
List<LEDRow> row = new ArrayList<LEDRow>();
46+
for(int j=0; j<4; j++){
47+
row.add(new LEDRow(x,y));
48+
x+=31;
49+
}
50+
y-=6;
51+
matrix.add(row);
52+
}
53+
54+
for (List<LEDRow> list : matrix){
55+
for (LEDRow led : list){
56+
addPart(led);
57+
}
58+
}
59+
60+
//addPart(new SSText(-45, 15, "LED Matrix", 40, Colors.moduleLabel));
61+
propagate();
62+
}
63+
64+
65+
@Override
66+
public AvailableModules getModType() {
67+
68+
return AvailableModules.LEDMatrix;
69+
}
70+
71+
@Override
72+
public void paint(Graphics2D g) {
73+
// Fill in polygon
74+
g.setColor(Colors.moduleFill);
75+
drawBox(g, 10);
76+
g.setColor(Colors.moduleInset);
77+
drawTrapezoid(g, 5, 0, 55, 130, 20);
78+
79+
// Show IO
80+
g.setColor(Colors.modulePorts);
81+
drawInputs(g);
82+
drawOutputs(g);
83+
84+
// Show LEDs
85+
drawParts(g);
86+
}
87+
88+
@Override
89+
public void propagate() {
90+
if(!persist){
91+
List<LEDRow> prevleds = matrix.get(previousRow);
92+
for(int i=0; i<4; i++){
93+
prevleds.get(i).setVal(new BinData(0));
94+
}
95+
}
96+
final int sel = contIn.getVal().getUInt() & 15;
97+
previousRow = sel;
98+
List<LEDRow> leds = matrix.get(sel);
99+
for(int i=0; i<4; i++){
100+
leds.get(i).setVal(dIn.get(i).getVal());
101+
}
102+
}
103+
104+
@Override
105+
public PickableEntity createNew() {
106+
return new LEDMatrix();
107+
}
108+
109+
public void turnOffPersist(){
110+
persist = false;
111+
for(List<LEDRow> row : matrix){
112+
for(LEDRow group : row){
113+
group.setVal(new BinData(0));
114+
}
115+
}
116+
}
117+
118+
public void turnOnPersist(){
119+
persist = true;
120+
}
121+
122+
public boolean isPersistEnabled(){
123+
return persist;
124+
}
125+
126+
@Override
127+
public HashMap<String, String> dataOut() {
128+
if (!isPersistEnabled()) return null;
129+
130+
HashMap<String, String> data = new HashMap<>();
131+
data.put("persist", "1");
132+
133+
return data;
134+
}
135+
136+
@Override
137+
public void dataIn(HashMap<String, String> data) {
138+
if (data.containsKey("persist")) {
139+
String storeStr = data.get("persist");
140+
try{
141+
if(Integer.parseInt(storeStr)==1)
142+
turnOnPersist();
143+
}catch(NumberFormatException e){
144+
//leave persist off
145+
}
146+
}
147+
}
148+
149+
}

src/com/modsim/modules/Or.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public class Or extends BaseModule {
2626

2727
Or() {
2828
w = 150;
29-
h = 50;
29+
h = 150;
3030

3131
// Output
3232
rOut = addOutput("Or'ed output", 0, Port.CTRL);
@@ -60,7 +60,7 @@ public BaseModule createNew() {
6060
public void paint(Graphics2D g) {
6161
// Fill in polygon
6262
g.setColor(Colors.moduleFill);
63-
drawTrapezoid(g, 10);
63+
drawBox(g, 10);
6464

6565
// Show output/input
6666
g.setColor(Colors.modulePorts);

0 commit comments

Comments
 (0)