org.eclipse.jdt.core.formatter.align_type_members_on_columns=false\r
org.eclipse.jdt.core.formatter.align_variable_declarations_on_columns=false\r
org.eclipse.jdt.core.formatter.align_with_spaces=false\r
-org.eclipse.jdt.core.formatter.alignment_for_additive_operator=16\r
org.eclipse.jdt.core.formatter.alignment_for_arguments_in_allocation_expression=16\r
org.eclipse.jdt.core.formatter.alignment_for_arguments_in_annotation=0\r
org.eclipse.jdt.core.formatter.alignment_for_arguments_in_enum_constant=16\r
org.eclipse.jdt.core.formatter.alignment_for_arguments_in_qualified_allocation_expression=16\r
org.eclipse.jdt.core.formatter.alignment_for_assignment=0\r
org.eclipse.jdt.core.formatter.alignment_for_binary_expression=16\r
-org.eclipse.jdt.core.formatter.alignment_for_bitwise_operator=16\r
org.eclipse.jdt.core.formatter.alignment_for_compact_if=16\r
org.eclipse.jdt.core.formatter.alignment_for_compact_loops=16\r
org.eclipse.jdt.core.formatter.alignment_for_conditional_expression=80\r
-org.eclipse.jdt.core.formatter.alignment_for_conditional_expression_chain=0\r
org.eclipse.jdt.core.formatter.alignment_for_enum_constants=16\r
org.eclipse.jdt.core.formatter.alignment_for_expressions_in_array_initializer=16\r
org.eclipse.jdt.core.formatter.alignment_for_expressions_in_for_loop_header=0\r
-org.eclipse.jdt.core.formatter.alignment_for_logical_operator=16\r
org.eclipse.jdt.core.formatter.alignment_for_method_declaration=0\r
org.eclipse.jdt.core.formatter.alignment_for_module_statements=16\r
org.eclipse.jdt.core.formatter.alignment_for_multiple_fields=16\r
-org.eclipse.jdt.core.formatter.alignment_for_multiplicative_operator=16\r
org.eclipse.jdt.core.formatter.alignment_for_parameterized_type_references=0\r
org.eclipse.jdt.core.formatter.alignment_for_parameters_in_constructor_declaration=16\r
org.eclipse.jdt.core.formatter.alignment_for_parameters_in_method_declaration=16\r
-org.eclipse.jdt.core.formatter.alignment_for_relational_operator=0\r
org.eclipse.jdt.core.formatter.alignment_for_resources_in_try=80\r
org.eclipse.jdt.core.formatter.alignment_for_selector_in_method_invocation=16\r
-org.eclipse.jdt.core.formatter.alignment_for_shift_operator=0\r
-org.eclipse.jdt.core.formatter.alignment_for_string_concatenation=16\r
org.eclipse.jdt.core.formatter.alignment_for_superclass_in_type_declaration=16\r
org.eclipse.jdt.core.formatter.alignment_for_superinterfaces_in_enum_declaration=16\r
org.eclipse.jdt.core.formatter.alignment_for_superinterfaces_in_type_declaration=16\r
org.eclipse.jdt.core.formatter.comment.format_source_code=true\r
org.eclipse.jdt.core.formatter.comment.indent_parameter_description=false\r
org.eclipse.jdt.core.formatter.comment.indent_root_tags=false\r
-org.eclipse.jdt.core.formatter.comment.indent_tag_description=false\r
org.eclipse.jdt.core.formatter.comment.insert_new_line_before_root_tags=insert\r
org.eclipse.jdt.core.formatter.comment.insert_new_line_for_parameter=do not insert\r
org.eclipse.jdt.core.formatter.comment.line_length=140\r
org.eclipse.jdt.core.formatter.insert_new_line_in_empty_enum_declaration=insert\r
org.eclipse.jdt.core.formatter.insert_new_line_in_empty_method_body=insert\r
org.eclipse.jdt.core.formatter.insert_new_line_in_empty_type_declaration=insert\r
-org.eclipse.jdt.core.formatter.insert_space_after_additive_operator=insert\r
org.eclipse.jdt.core.formatter.insert_space_after_and_in_type_parameter=insert\r
org.eclipse.jdt.core.formatter.insert_space_after_assignment_operator=insert\r
org.eclipse.jdt.core.formatter.insert_space_after_at_in_annotation=do not insert\r
org.eclipse.jdt.core.formatter.insert_space_after_at_in_annotation_type_declaration=do not insert\r
org.eclipse.jdt.core.formatter.insert_space_after_binary_operator=insert\r
-org.eclipse.jdt.core.formatter.insert_space_after_bitwise_operator=insert\r
org.eclipse.jdt.core.formatter.insert_space_after_closing_angle_bracket_in_type_arguments=do not insert\r
org.eclipse.jdt.core.formatter.insert_space_after_closing_angle_bracket_in_type_parameters=insert\r
org.eclipse.jdt.core.formatter.insert_space_after_closing_brace_in_block=insert\r
org.eclipse.jdt.core.formatter.insert_space_after_comma_in_type_parameters=insert\r
org.eclipse.jdt.core.formatter.insert_space_after_ellipsis=insert\r
org.eclipse.jdt.core.formatter.insert_space_after_lambda_arrow=insert\r
-org.eclipse.jdt.core.formatter.insert_space_after_logical_operator=insert\r
-org.eclipse.jdt.core.formatter.insert_space_after_multiplicative_operator=insert\r
org.eclipse.jdt.core.formatter.insert_space_after_opening_angle_bracket_in_parameterized_type_reference=do not insert\r
org.eclipse.jdt.core.formatter.insert_space_after_opening_angle_bracket_in_type_arguments=do not insert\r
org.eclipse.jdt.core.formatter.insert_space_after_opening_angle_bracket_in_type_parameters=do not insert\r
org.eclipse.jdt.core.formatter.insert_space_after_prefix_operator=do not insert\r
org.eclipse.jdt.core.formatter.insert_space_after_question_in_conditional=insert\r
org.eclipse.jdt.core.formatter.insert_space_after_question_in_wildcard=do not insert\r
-org.eclipse.jdt.core.formatter.insert_space_after_relational_operator=insert\r
org.eclipse.jdt.core.formatter.insert_space_after_semicolon_in_for=insert\r
org.eclipse.jdt.core.formatter.insert_space_after_semicolon_in_try_resources=insert\r
-org.eclipse.jdt.core.formatter.insert_space_after_shift_operator=insert\r
-org.eclipse.jdt.core.formatter.insert_space_after_string_concatenation=insert\r
org.eclipse.jdt.core.formatter.insert_space_after_unary_operator=do not insert\r
-org.eclipse.jdt.core.formatter.insert_space_before_additive_operator=insert\r
org.eclipse.jdt.core.formatter.insert_space_before_and_in_type_parameter=insert\r
org.eclipse.jdt.core.formatter.insert_space_before_assignment_operator=insert\r
org.eclipse.jdt.core.formatter.insert_space_before_at_in_annotation_type_declaration=insert\r
org.eclipse.jdt.core.formatter.insert_space_before_binary_operator=insert\r
-org.eclipse.jdt.core.formatter.insert_space_before_bitwise_operator=insert\r
org.eclipse.jdt.core.formatter.insert_space_before_closing_angle_bracket_in_parameterized_type_reference=do not insert\r
org.eclipse.jdt.core.formatter.insert_space_before_closing_angle_bracket_in_type_arguments=do not insert\r
org.eclipse.jdt.core.formatter.insert_space_before_closing_angle_bracket_in_type_parameters=do not insert\r
org.eclipse.jdt.core.formatter.insert_space_before_comma_in_type_parameters=do not insert\r
org.eclipse.jdt.core.formatter.insert_space_before_ellipsis=do not insert\r
org.eclipse.jdt.core.formatter.insert_space_before_lambda_arrow=insert\r
-org.eclipse.jdt.core.formatter.insert_space_before_logical_operator=insert\r
-org.eclipse.jdt.core.formatter.insert_space_before_multiplicative_operator=insert\r
org.eclipse.jdt.core.formatter.insert_space_before_opening_angle_bracket_in_parameterized_type_reference=do not insert\r
org.eclipse.jdt.core.formatter.insert_space_before_opening_angle_bracket_in_type_arguments=do not insert\r
org.eclipse.jdt.core.formatter.insert_space_before_opening_angle_bracket_in_type_parameters=do not insert\r
org.eclipse.jdt.core.formatter.insert_space_before_prefix_operator=do not insert\r
org.eclipse.jdt.core.formatter.insert_space_before_question_in_conditional=insert\r
org.eclipse.jdt.core.formatter.insert_space_before_question_in_wildcard=do not insert\r
-org.eclipse.jdt.core.formatter.insert_space_before_relational_operator=insert\r
org.eclipse.jdt.core.formatter.insert_space_before_semicolon=do not insert\r
org.eclipse.jdt.core.formatter.insert_space_before_semicolon_in_for=do not insert\r
org.eclipse.jdt.core.formatter.insert_space_before_semicolon_in_try_resources=do not insert\r
-org.eclipse.jdt.core.formatter.insert_space_before_shift_operator=insert\r
-org.eclipse.jdt.core.formatter.insert_space_before_string_concatenation=insert\r
org.eclipse.jdt.core.formatter.insert_space_before_unary_operator=do not insert\r
org.eclipse.jdt.core.formatter.insert_space_between_brackets_in_array_type_reference=do not insert\r
org.eclipse.jdt.core.formatter.insert_space_between_empty_braces_in_array_initializer=do not insert\r
org.eclipse.jdt.core.formatter.insert_space_between_empty_parens_in_method_invocation=do not insert\r
org.eclipse.jdt.core.formatter.join_lines_in_comments=true\r
org.eclipse.jdt.core.formatter.join_wrapped_lines=true\r
-org.eclipse.jdt.core.formatter.keep_annotation_declaration_on_one_line=one_line_never\r
-org.eclipse.jdt.core.formatter.keep_anonymous_type_declaration_on_one_line=one_line_never\r
-org.eclipse.jdt.core.formatter.keep_code_block_on_one_line=one_line_never\r
org.eclipse.jdt.core.formatter.keep_else_statement_on_same_line=false\r
org.eclipse.jdt.core.formatter.keep_empty_array_initializer_on_one_line=false\r
-org.eclipse.jdt.core.formatter.keep_enum_constant_declaration_on_one_line=one_line_never\r
-org.eclipse.jdt.core.formatter.keep_enum_declaration_on_one_line=one_line_never\r
-org.eclipse.jdt.core.formatter.keep_if_then_body_block_on_one_line=one_line_never\r
org.eclipse.jdt.core.formatter.keep_imple_if_on_one_line=false\r
-org.eclipse.jdt.core.formatter.keep_lambda_body_block_on_one_line=one_line_never\r
-org.eclipse.jdt.core.formatter.keep_loop_body_block_on_one_line=one_line_never\r
-org.eclipse.jdt.core.formatter.keep_method_body_on_one_line=one_line_never\r
org.eclipse.jdt.core.formatter.keep_simple_do_while_body_on_same_line=false\r
org.eclipse.jdt.core.formatter.keep_simple_for_body_on_same_line=false\r
-org.eclipse.jdt.core.formatter.keep_simple_getter_setter_on_one_line=false\r
org.eclipse.jdt.core.formatter.keep_simple_while_body_on_same_line=false\r
org.eclipse.jdt.core.formatter.keep_then_statement_on_same_line=false\r
-org.eclipse.jdt.core.formatter.keep_type_declaration_on_one_line=one_line_never\r
org.eclipse.jdt.core.formatter.lineSplit=140\r
org.eclipse.jdt.core.formatter.never_indent_block_comments_on_first_column=false\r
org.eclipse.jdt.core.formatter.never_indent_line_comments_on_first_column=false\r
org.eclipse.jdt.core.formatter.tabulation.size=4\r
org.eclipse.jdt.core.formatter.use_on_off_tags=true\r
org.eclipse.jdt.core.formatter.use_tabs_only_for_leading_indentations=false\r
-org.eclipse.jdt.core.formatter.wrap_before_additive_operator=true\r
org.eclipse.jdt.core.formatter.wrap_before_assignment_operator=false\r
org.eclipse.jdt.core.formatter.wrap_before_binary_operator=true\r
-org.eclipse.jdt.core.formatter.wrap_before_bitwise_operator=true\r
org.eclipse.jdt.core.formatter.wrap_before_conditional_operator=true\r
-org.eclipse.jdt.core.formatter.wrap_before_logical_operator=true\r
-org.eclipse.jdt.core.formatter.wrap_before_multiplicative_operator=true\r
org.eclipse.jdt.core.formatter.wrap_before_or_operator_multicatch=true\r
-org.eclipse.jdt.core.formatter.wrap_before_relational_operator=true\r
-org.eclipse.jdt.core.formatter.wrap_before_shift_operator=true\r
-org.eclipse.jdt.core.formatter.wrap_before_string_concatenation=true\r
org.eclipse.jdt.core.formatter.wrap_outer_expressions_when_nested=true\r
org.eclipse.jdt.core.javaFormatter=org.eclipse.jdt.core.defaultJavaFormatter\r
eclipse.preferences.version=1\r
-editor_save_participant_org.eclipse.jdt.ui.postsavelistener.cleanup=true\r
-formatter_profile=_ERA-MI\r
-formatter_settings_version=16\r
-sp_cleanup.add_default_serial_version_id=true\r
-sp_cleanup.add_generated_serial_version_id=false\r
-sp_cleanup.add_missing_annotations=true\r
-sp_cleanup.add_missing_deprecated_annotations=true\r
-sp_cleanup.add_missing_methods=false\r
-sp_cleanup.add_missing_nls_tags=false\r
-sp_cleanup.add_missing_override_annotations=true\r
-sp_cleanup.add_missing_override_annotations_interface_methods=true\r
-sp_cleanup.add_serial_version_id=false\r
-sp_cleanup.always_use_blocks=false\r
-sp_cleanup.always_use_parentheses_in_expressions=false\r
-sp_cleanup.always_use_this_for_non_static_field_access=false\r
-sp_cleanup.always_use_this_for_non_static_method_access=false\r
-sp_cleanup.convert_functional_interfaces=true\r
-sp_cleanup.convert_to_enhanced_for_loop=true\r
-sp_cleanup.correct_indentation=true\r
-sp_cleanup.format_source_code=true\r
-sp_cleanup.format_source_code_changes_only=false\r
-sp_cleanup.insert_inferred_type_arguments=false\r
-sp_cleanup.make_local_variable_final=true\r
-sp_cleanup.make_parameters_final=false\r
-sp_cleanup.make_private_fields_final=true\r
-sp_cleanup.make_type_abstract_if_missing_method=false\r
-sp_cleanup.make_variable_declarations_final=true\r
-sp_cleanup.never_use_blocks=true\r
-sp_cleanup.never_use_parentheses_in_expressions=true\r
-sp_cleanup.on_save_use_additional_actions=false\r
-sp_cleanup.organize_imports=true\r
-sp_cleanup.qualify_static_field_accesses_with_declaring_class=false\r
-sp_cleanup.qualify_static_member_accesses_through_instances_with_declaring_class=true\r
-sp_cleanup.qualify_static_member_accesses_through_subtypes_with_declaring_class=true\r
-sp_cleanup.qualify_static_member_accesses_with_declaring_class=true\r
-sp_cleanup.qualify_static_method_accesses_with_declaring_class=false\r
-sp_cleanup.remove_private_constructors=true\r
-sp_cleanup.remove_redundant_modifiers=true\r
-sp_cleanup.remove_redundant_semicolons=true\r
-sp_cleanup.remove_redundant_type_arguments=true\r
-sp_cleanup.remove_trailing_whitespaces=true\r
-sp_cleanup.remove_trailing_whitespaces_all=true\r
-sp_cleanup.remove_trailing_whitespaces_ignore_empty=false\r
-sp_cleanup.remove_unnecessary_casts=true\r
-sp_cleanup.remove_unnecessary_nls_tags=true\r
-sp_cleanup.remove_unused_imports=true\r
-sp_cleanup.remove_unused_local_variables=false\r
-sp_cleanup.remove_unused_private_fields=true\r
-sp_cleanup.remove_unused_private_members=false\r
-sp_cleanup.remove_unused_private_methods=true\r
-sp_cleanup.remove_unused_private_types=true\r
-sp_cleanup.sort_members=false\r
-sp_cleanup.sort_members_all=false\r
-sp_cleanup.use_anonymous_class_creation=false\r
-sp_cleanup.use_blocks=false\r
-sp_cleanup.use_blocks_only_for_return_and_throw=false\r
-sp_cleanup.use_lambda=true\r
-sp_cleanup.use_parentheses_in_expressions=false\r
-sp_cleanup.use_this_for_non_static_field_access=true\r
-sp_cleanup.use_this_for_non_static_field_access_only_if_necessary=true\r
-sp_cleanup.use_this_for_non_static_method_access=true\r
-sp_cleanup.use_this_for_non_static_method_access_only_if_necessary=true\r
+formatter_profile=_MyPrefrence\r
+formatter_settings_version=14\r
}\r
\r
// @formatter:off\r
- private static final Bit[][] JOIN_TABLE = \r
+ private static Bit[][] JOIN_TABLE = \r
{ { U, U, U, U, U }, \r
{ U, X, X, X, X }, \r
{ U, X, ZERO, X, ZERO },\r
{ U, X, X, ONE, ONE }, \r
{ U, X, ZERO, ONE, Z } };\r
\r
- private static final Bit[][] AND_TABLE = \r
+ private static Bit[][] AND_TABLE = \r
{ { U, U, ZERO, U, U }, \r
{ U, X, ZERO, X, X },\r
{ ZERO, ZERO, ZERO, ZERO, ZERO }, \r
{ U, X, ZERO, ONE, X }, \r
{ U, X, ZERO, X, X } };\r
\r
- private static final Bit[][] OR_TABLE =\r
+ private static Bit[][] OR_TABLE =\r
{ { U, U, U, ONE, U }, \r
{ U, X, X, ONE, X }, \r
{ U, X, ZERO, ONE, X }, \r
{ ONE, ONE, ONE, ONE, ONE }, \r
{ U, X, X, ONE, X } };\r
\r
- private static final Bit[][] XOR_TABLE =\r
+ private static Bit[][] XOR_TABLE =\r
{ { U, U, U, U, U }, \r
{ U, X, X, X, X }, \r
{ U, X, ZERO, ONE, X }, \r
{\r
public final static Timeline TIMELINE = new Timeline(11);\r
\r
+ public static void main(String[] args)\r
+ {\r
+ }\r
}
\ No newline at end of file
\r
import era.mi.logic.Bit;\r
import era.mi.logic.Simulation;\r
-import era.mi.logic.wires.WireArray;\r
+import era.mi.logic.wires.Wire;\r
import era.mi.logic.wires.WireArrayObserver;\r
\r
/**\r
}\r
\r
@Override\r
- public void update(WireArray initiator, Bit[] oldValues)\r
+ public void update(Wire initiator, Bit[] oldValues)\r
{\r
- Simulation.TIMELINE.addEvent(e -> compute(), processTime);\r
+ Simulation.TIMELINE.addEvent((e) ->\r
+ {\r
+ compute();\r
+ }, processTime);\r
}\r
\r
protected abstract void compute();\r
package era.mi.logic.components;\r
\r
+import java.util.ArrayList;\r
import java.util.Arrays;\r
+import java.util.Collections;\r
import java.util.List;\r
\r
import era.mi.logic.Bit;\r
-import era.mi.logic.wires.WireArray;\r
+import era.mi.logic.wires.Wire.WireEnd;\r
\r
public class BitDisplay extends BasicComponent\r
{\r
- private final WireArray in;\r
+ private final WireEnd in;\r
private Bit[] displayedValue;\r
\r
- public BitDisplay(WireArray in)\r
+ public BitDisplay(WireEnd in)\r
{\r
super(1);\r
this.in = in;\r
}\r
\r
@Override\r
- public List<WireArray> getAllInputs()\r
+ public List<WireEnd> getAllInputs()\r
{\r
- return List.of(in);\r
+ return Collections.unmodifiableList(Arrays.asList(in));\r
}\r
\r
@Override\r
- public List<WireArray> getAllOutputs()\r
+ public List<WireEnd> getAllOutputs()\r
{\r
- return List.of();\r
+ return Collections.unmodifiableList(new ArrayList<WireEnd>());\r
}\r
}\r
package era.mi.logic.components;\r
\r
+import java.util.Arrays;\r
+import java.util.Collections;\r
import java.util.List;\r
\r
import era.mi.logic.Bit;\r
import era.mi.logic.Simulation;\r
import era.mi.logic.timeline.TimelineEvent;\r
import era.mi.logic.timeline.TimelineEventHandler;\r
-import era.mi.logic.wires.WireArray;\r
-import era.mi.logic.wires.WireArray.WireArrayEnd;\r
+import era.mi.logic.wires.Wire;\r
+import era.mi.logic.wires.Wire.WireEnd;\r
\r
public class Clock implements TimelineEventHandler, Component\r
{\r
private boolean toggle = false;\r
- private WireArrayEnd outI;\r
+ private WireEnd out;\r
private int delta;\r
\r
/**\r
* \r
- * @param out {@link WireArray} the clock's impulses are fed into\r
+ * @param out {@link Wire} the clock's impulses are fed into\r
* @param delta ticks between rising and falling edge\r
*/\r
- public Clock(WireArray out, int delta)\r
+ public Clock(WireEnd out, int delta)\r
{\r
this.delta = delta;\r
- this.outI = out.createInput();\r
- Simulation.TIMELINE.addEvent(this, delta);\r
+ this.out = out;\r
+ addToTimeline();\r
}\r
\r
@Override\r
public void handle(TimelineEvent e)\r
{\r
addToTimeline();\r
- outI.feedSignals(toggle ? Bit.ONE : Bit.ZERO);\r
+ out.feedSignals(new Bit[] { toggle ? Bit.ONE : Bit.ZERO });\r
toggle = !toggle;\r
}\r
\r
- public WireArray getOut()\r
+ public WireEnd getOut()\r
{\r
- return outI.owner;\r
+ return out;\r
}\r
\r
private void addToTimeline()\r
}\r
\r
@Override\r
- public List<WireArray> getAllInputs()\r
+ public List<WireEnd> getAllInputs()\r
{\r
- return List.of();\r
+ return Collections.unmodifiableList(Arrays.asList());\r
}\r
\r
@Override\r
- public List<WireArray> getAllOutputs()\r
+ public List<WireEnd> getAllOutputs()\r
{\r
- return List.of(outI.owner);\r
+ return Collections.unmodifiableList(Arrays.asList(out));\r
}\r
}\r
\r
import java.util.List;\r
\r
-import era.mi.logic.wires.WireArray;\r
+import era.mi.logic.wires.Wire.WireEnd;\r
\r
public interface Component\r
{\r
* Returns immutable list of all inputs to the {@link Component} (including e.g. the select bits to a MUX). Intended for visualization\r
* in the UI.\r
*/\r
- public List<WireArray> getAllInputs();\r
+ public List<WireEnd> getAllInputs();\r
\r
/**\r
* Returns immutable list of all outputs to the {@link Component}. Intended for visualization in the UI.\r
*/\r
- public List<WireArray> getAllOutputs();\r
+ public List<WireEnd> getAllOutputs();\r
}\r
+++ /dev/null
-package era.mi.logic.components;\r
-\r
-import java.util.List;\r
-\r
-import era.mi.logic.Bit;\r
-import era.mi.logic.Simulation;\r
-import era.mi.logic.wires.WireArray;\r
-import era.mi.logic.wires.WireArray.WireArrayEnd;\r
-import era.mi.logic.wires.WireArrayObserver;\r
-\r
-public class Connector implements WireArrayObserver, Component\r
-{\r
- private boolean connected;\r
- private final WireArray a;\r
- private final WireArray b;\r
- private final WireArrayEnd aI;\r
- private final WireArrayEnd bI;\r
-\r
- public Connector(WireArray a, WireArray b)\r
- {\r
- if (a.length != b.length)\r
- throw new IllegalArgumentException(String.format("WireArray width does not match: %d, %d", a.length, b.length));\r
- this.a = a;\r
- this.b = b;\r
- a.addObserver(this);\r
- b.addObserver(this);\r
- aI = a.createInput();\r
- bI = b.createInput();\r
- }\r
-\r
- public void connect()\r
- {\r
- connected = true;\r
- update(a);\r
- update(b);\r
- }\r
-\r
- public void disconnect()\r
- {\r
- connected = false;\r
- aI.clearSignals();\r
- bI.clearSignals();\r
- }\r
-\r
- public void setConnection(boolean connected)\r
- {\r
- if (connected)\r
- connect();\r
- else\r
- disconnect();\r
- }\r
-\r
- @Override\r
- public void update(WireArray initiator, Bit[] oldValues)\r
- {\r
- if (connected)\r
- Simulation.TIMELINE.addEvent(e -> update(initiator), 1);\r
- }\r
-\r
- private void update(WireArray initiator)\r
- {\r
- if (initiator == a)\r
- bI.feedSignals(aI.wireValuesExcludingMe());\r
- else\r
- aI.feedSignals(bI.wireValuesExcludingMe());\r
- }\r
-\r
- @Override\r
- public List<WireArray> getAllInputs()\r
- {\r
- return List.of(a, b);\r
- }\r
-\r
- @Override\r
- public List<WireArray> getAllOutputs()\r
- {\r
- return List.of(a, b);\r
- }\r
-}\r
package era.mi.logic.components;\r
\r
+import java.util.Arrays;\r
+import java.util.Collections;\r
import java.util.List;\r
\r
-import era.mi.logic.wires.WireArray;\r
-import era.mi.logic.wires.WireArray.WireArrayEnd;\r
+import era.mi.logic.wires.Wire;\r
+import era.mi.logic.wires.Wire.WireEnd;\r
\r
/**\r
- * Models a multiplexer. Takes an arbitrary amount of outputs {@link WireArray}s, one of which, as determined by select, receives the input\r
- * signal.\r
+ * Models a multiplexer. Takes an arbitrary amount of input {@link Wire}s, one of which, as determined by select, is put through to the\r
+ * output.\r
* \r
* @author Fabian Stemmler\r
*\r
*/\r
public class Demux extends BasicComponent\r
{\r
- private final WireArray select, in;\r
- private final WireArray[] outputs;\r
- private final WireArrayEnd[] outputsI;\r
+ private final WireEnd select, in;\r
+ private final WireEnd[] outputs;\r
private final int outputSize;\r
private int selected = -1;\r
\r
/**\r
- * Output {@link WireArray}s and in must be of uniform length\r
+ * Input {@link Wire}s and out must be of uniform length\r
* \r
- * @param in Must be of uniform length with all outputs.\r
- * @param select Indexes the output array to which the input is mapped. Must have enough bits to index all outputs.\r
- * @param outputs One of these outputs receives the input signal, depending on the select bits\r
+ * @param out Must be of uniform length with all inputs.\r
+ * @param select Indexes the input array which is to be mapped to the output. Must have enough bits to index all inputs.\r
+ * @param outputs One of these inputs is mapped to the output, depending on the select bits\r
*/\r
- public Demux(int processTime, WireArray in, WireArray select, WireArray... outputs)\r
+ public Demux(int processTime, WireEnd in, WireEnd select, WireEnd... outputs)\r
{\r
super(processTime);\r
- outputSize = in.length;\r
+ outputSize = in.length();\r
\r
this.in = in;\r
this.outputs = outputs;\r
- this.outputsI = new WireArrayEnd[outputs.length];\r
- for (int i = 0; i < this.outputsI.length; i++)\r
+ for (int i = 0; i < this.outputs.length; i++)\r
{\r
- if (outputs[i].length != outputSize)\r
+ if (outputs[i].length() != outputSize)\r
throw new IllegalArgumentException("All DEMUX wire arrays must be of uniform length!");\r
- this.outputsI[i] = outputs[i].createInput();\r
+ this.outputs[i] = outputs[i];\r
}\r
\r
this.select = select;\r
select.addObserver(this);\r
\r
- int maxInputs = 1 << select.length;\r
- if (this.outputsI.length > maxInputs)\r
- throw new IllegalArgumentException("There are more outputs (" + this.outputsI.length + ") to the DEMUX than supported by "\r
- + select.length + " select bits (" + maxInputs + ").");\r
+ int maxInputs = 1 << select.length();\r
+ if (this.outputs.length > maxInputs)\r
+ throw new IllegalArgumentException("There are more outputs (" + this.outputs.length + ") to the DEMUX than supported by "\r
+ + select.length() + " select bits (" + maxInputs + ").");\r
in.addObserver(this);\r
}\r
\r
public void compute()\r
{\r
int selectValue = select.hasNumericValue() ? (int) select.getUnsignedValue() : -1;\r
- if (selectValue >= outputsI.length)\r
+ if (selectValue >= outputs.length)\r
selectValue = -1;\r
\r
if (selected != selectValue && selected != -1)\r
- outputsI[selected].clearSignals();\r
+ outputs[selected].clearSignals();\r
\r
selected = selectValue;\r
\r
if (selectValue != -1)\r
- outputsI[selectValue].feedSignals(in.getValues());\r
+ outputs[selectValue].feedSignals(in.getValues());\r
}\r
\r
@Override\r
- public List<WireArray> getAllInputs()\r
+ public List<WireEnd> getAllInputs()\r
{\r
- return List.of(in, select);\r
+ return Collections.unmodifiableList(Arrays.asList(in, select));\r
}\r
\r
@Override\r
- public List<WireArray> getAllOutputs()\r
+ public List<WireEnd> getAllOutputs()\r
{\r
- return List.of(outputs);\r
+ return Collections.unmodifiableList(Arrays.asList(outputs));\r
}\r
}\r
import java.util.List;\r
\r
import era.mi.logic.Bit;\r
-import era.mi.logic.wires.WireArray;\r
-import era.mi.logic.wires.WireArray.WireArrayEnd;\r
+import era.mi.logic.wires.Wire.WireEnd;\r
\r
/**\r
* This class models a simple on/off (ONE/ZERO) switch for user interaction.\r
*/\r
public class ManualSwitch implements Component\r
{\r
- private WireArray output;\r
- private WireArrayEnd outputI;\r
+ private WireEnd output;\r
private boolean isOn;\r
\r
- public ManualSwitch(WireArray output)\r
+ public ManualSwitch(WireEnd output)\r
{\r
- if (output.length != 1)\r
+ if (output.length() != 1)\r
throw new IllegalArgumentException("Switch output can be only a single wire");\r
this.output = output;\r
- this.outputI = output.createInput();\r
}\r
\r
public void switchOn()\r
if (this.isOn == isOn)\r
return;\r
this.isOn = isOn;\r
- outputI.feedSignals(getValue());\r
+ output.feedSignals(getValue());\r
}\r
\r
public boolean isOn()\r
}\r
\r
@Override\r
- public List<WireArray> getAllInputs()\r
+ public List<WireEnd> getAllInputs()\r
{\r
return List.of();\r
}\r
\r
@Override\r
- public List<WireArray> getAllOutputs()\r
+ public List<WireEnd> getAllOutputs()\r
{\r
return List.of(output);\r
}\r
package era.mi.logic.components;\r
\r
+import java.util.Arrays;\r
+import java.util.Collections;\r
import java.util.List;\r
\r
import era.mi.logic.Bit;\r
-import era.mi.logic.wires.WireArray;\r
-import era.mi.logic.wires.WireArray.WireArrayEnd;\r
+import era.mi.logic.wires.Wire;\r
+import era.mi.logic.wires.Wire.WireEnd;\r
import era.mi.logic.wires.WireArrayObserver;\r
\r
public class Merger implements WireArrayObserver, Component\r
{\r
- private WireArrayEnd outI;\r
- private WireArray[] inputs;\r
+ private WireEnd out;\r
+ private WireEnd[] inputs;\r
private int[] beginningIndex;\r
\r
/**\r
* \r
- * @param union The output of merging n {@link WireArray}s into one. Must have length = a1.length() + a2.length() + ... + an.length().\r
+ * @param union The output of merging n {@link Wire}s into one. Must have length = a1.length() + a2.length() + ... + an.length().\r
* @param inputs The inputs to be merged into the union\r
*/\r
- public Merger(WireArray union, WireArray... inputs)\r
+ public Merger(WireEnd union, WireEnd... inputs)\r
{\r
this.inputs = inputs;\r
- this.outI = union.createInput();\r
+ this.out = union;\r
this.beginningIndex = new int[inputs.length];\r
\r
int length = 0;\r
for (int i = 0; i < inputs.length; i++)\r
{\r
beginningIndex[i] = length;\r
- length += inputs[i].length;\r
+ length += inputs[i].length();\r
inputs[i].addObserver(this);\r
}\r
\r
- if (length != union.length)\r
+ if (length != union.length())\r
throw new IllegalArgumentException(\r
"The output of merging n WireArrays into one must have length = a1.length() + a2.length() + ... + an.length().");\r
}\r
\r
- public WireArray getInput(int index)\r
+ public WireEnd getInput(int index)\r
{\r
return inputs[index];\r
}\r
\r
- public WireArray getUnion()\r
+ public WireEnd getUnion()\r
{\r
- return outI.owner;\r
+ return out;\r
}\r
\r
@Override\r
- public void update(WireArray initiator, Bit[] oldValues)\r
+ public void update(Wire initiator, Bit[] oldValues)\r
{\r
int index = find(initiator);\r
int beginning = beginningIndex[index];\r
- outI.feedSignals(beginning, initiator.getValues());\r
+ out.feedSignals(beginning, inputs[index].getValues());\r
}\r
\r
- private int find(WireArray w)\r
+ private int find(Wire w)\r
{\r
for (int i = 0; i < inputs.length; i++)\r
- if (inputs[i] == w)\r
+ if (inputs[i].getWire() == w)\r
return i;\r
return -1;\r
}\r
\r
- public WireArray[] getInputs()\r
+ public WireEnd[] getInputs()\r
{\r
return inputs.clone();\r
}\r
\r
@Override\r
- public List<WireArray> getAllInputs()\r
+ public List<WireEnd> getAllInputs()\r
{\r
- return List.of(inputs);\r
+ return Collections.unmodifiableList(Arrays.asList(inputs));\r
}\r
\r
@Override\r
- public List<WireArray> getAllOutputs()\r
+ public List<WireEnd> getAllOutputs()\r
{\r
- return List.of(outI.owner);\r
+ return Collections.unmodifiableList(Arrays.asList(out));\r
}\r
}\r
import java.util.Collections;\r
import java.util.List;\r
\r
-import era.mi.logic.wires.WireArray;\r
-import era.mi.logic.wires.WireArray.WireArrayEnd;\r
+import era.mi.logic.wires.Wire;\r
+import era.mi.logic.wires.Wire.WireEnd;\r
\r
/**\r
- * Models a multiplexer. Takes an arbitrary amount of input {@link WireArray}s, one of which, as determined by select, is put through to the\r
+ * Models a multiplexer. Takes an arbitrary amount of input {@link Wire}s, one of which, as determined by select, is put through to the\r
* output.\r
* \r
* @author Fabian Stemmler\r
*/\r
public class Mux extends BasicComponent\r
{\r
- private WireArray select;\r
- private WireArrayEnd outI;\r
- private WireArray[] inputs;\r
+ private WireEnd select;\r
+ private WireEnd out;\r
+ private WireEnd[] inputs;\r
private final int outputSize;\r
\r
/**\r
- * Input {@link WireArray}s and out must be of uniform length\r
+ * Input {@link Wire}s and out must be of uniform length\r
* \r
* @param out Must be of uniform length with all inputs.\r
* @param select Indexes the input array which is to be mapped to the output. Must have enough bits to index all inputs.\r
* @param inputs One of these inputs is mapped to the output, depending on the select bits\r
*/\r
- public Mux(int processTime, WireArray out, WireArray select, WireArray... inputs)\r
+ public Mux(int processTime, WireEnd out, WireEnd select, WireEnd... inputs)\r
{\r
super(processTime);\r
- outputSize = out.length;\r
+ outputSize = out.length();\r
\r
this.inputs = inputs.clone();\r
for (int i = 0; i < this.inputs.length; i++)\r
{\r
- if (inputs[i].length != outputSize)\r
+ if (inputs[i].length() != outputSize)\r
throw new IllegalArgumentException("All MUX wire arrays must be of uniform length!");\r
inputs[i].addObserver(this);\r
}\r
this.select = select;\r
select.addObserver(this);\r
\r
- int maxInputs = 1 << select.length;\r
+ int maxInputs = 1 << select.length();\r
if (this.inputs.length > maxInputs)\r
throw new IllegalArgumentException("There are more inputs (" + this.inputs.length + ") to the MUX than supported by "\r
- + select.length + " select bits (" + maxInputs + ").");\r
+ + select.length() + " select bits (" + maxInputs + ").");\r
\r
- outI = out.createInput();\r
+ this.out = out;\r
}\r
\r
- public WireArray getOut()\r
+ public WireEnd getOut()\r
{\r
- return outI.owner;\r
+ return out;\r
}\r
\r
- public WireArray getSelect()\r
+ public WireEnd getSelect()\r
{\r
return select;\r
}\r
int selectValue;\r
if (!select.hasNumericValue() || (selectValue = (int) select.getUnsignedValue()) >= inputs.length)\r
{\r
- outI.clearSignals();\r
+ out.clearSignals();\r
return;\r
}\r
\r
- WireArray active = inputs[selectValue];\r
- outI.feedSignals(active.getValues());\r
+ WireEnd active = inputs[selectValue];\r
+ out.feedSignals(active.getValues());\r
}\r
\r
@Override\r
- public List<WireArray> getAllInputs()\r
+ public List<WireEnd> getAllInputs()\r
{\r
- ArrayList<WireArray> wires = new ArrayList<WireArray>(Arrays.asList(inputs));\r
+ ArrayList<WireEnd> wires = new ArrayList<WireEnd>(Arrays.asList(inputs));\r
wires.add(select);\r
return Collections.unmodifiableList(wires);\r
}\r
\r
@Override\r
- public List<WireArray> getAllOutputs()\r
+ public List<WireEnd> getAllOutputs()\r
{\r
- return List.of(outI.owner);\r
+ return Collections.unmodifiableList(Arrays.asList(out));\r
}\r
}\r
package era.mi.logic.components;\r
\r
import era.mi.logic.Bit;\r
-import era.mi.logic.wires.WireArray;\r
-import era.mi.logic.wires.WireArray.WireArrayEnd;\r
+import era.mi.logic.wires.Wire;\r
+import era.mi.logic.wires.Wire.WireEnd;\r
import era.mi.logic.wires.WireArrayObserver;\r
\r
public class Splitter implements WireArrayObserver\r
{\r
- private WireArray input;\r
- private WireArrayEnd[] outputs;\r
+ private WireEnd input;\r
+ private WireEnd[] outputs;\r
\r
- public Splitter(WireArray input, WireArray... outputs)\r
+ public Splitter(WireEnd input, WireEnd... outputs)\r
{\r
this.input = input;\r
- this.outputs = WireArray.extractInputs(outputs);\r
+ this.outputs = outputs;\r
input.addObserver(this);\r
int length = 0;\r
- for (WireArray out : outputs)\r
- length += out.length;\r
+ for (WireEnd out : outputs)\r
+ length += out.length();\r
\r
- if (input.length != length)\r
+ if (input.length() != length)\r
throw new IllegalArgumentException(\r
"The input of splitting one into n WireArrays must have length = a1.length() + a2.length() + ... + an.length().");\r
}\r
Bit[] inputBits = input.getValues();\r
for (int i = 0; i < outputs.length; i++)\r
{\r
- Bit[] outputBits = new Bit[outputs[i].owner.length];\r
- System.arraycopy(inputBits, startIndex, outputBits, 0, outputs[i].owner.length);\r
+ Bit[] outputBits = new Bit[outputs[i].length()];\r
+ System.arraycopy(inputBits, startIndex, outputBits, 0, outputs[i].length());\r
outputs[i].feedSignals(outputBits);\r
- startIndex += outputs[i].owner.length;\r
+ startIndex += outputs[i].length();\r
}\r
}\r
\r
@Override\r
- public void update(WireArray initiator, Bit[] oldValues)\r
+ public void update(Wire initiator, Bit[] oldValues)\r
{\r
compute();\r
}\r
package era.mi.logic.components;\r
\r
+import java.util.Arrays;\r
+import java.util.Collections;\r
import java.util.List;\r
\r
import era.mi.logic.Bit;\r
-import era.mi.logic.wires.WireArray;\r
-import era.mi.logic.wires.WireArray.WireArrayEnd;\r
+import era.mi.logic.wires.Wire;\r
+import era.mi.logic.wires.Wire.WireEnd;\r
\r
public class TriStateBuffer extends BasicComponent\r
{\r
- WireArray in, enable;\r
- WireArrayEnd outI;\r
+ WireEnd in, enable;\r
+ WireEnd out;\r
\r
- public TriStateBuffer(int processTime, WireArray in, WireArray out, WireArray enable)\r
+ public TriStateBuffer(int processTime, WireEnd in, WireEnd out, WireEnd enable)\r
{\r
super(processTime);\r
- if (in.length != out.length)\r
+ if (in.length() != out.length())\r
throw new IllegalArgumentException(\r
- "Tri-state output must have the same amount of bits as the input. Input: " + in.length + " Output: " + out.length);\r
- if (enable.length != 1)\r
- throw new IllegalArgumentException("Tri-state enable must have exactly one bit, not " + enable.length + ".");\r
+ "Tri-state output must have the same amount of bits as the input. Input: " + in.length() + " Output: " + out.length());\r
+ if (enable.length() != 1)\r
+ throw new IllegalArgumentException("Tri-state enable must have exactly one bit, not " + enable.length() + ".");\r
this.in = in;\r
in.addObserver(this);\r
this.enable = enable;\r
enable.addObserver(this);\r
- outI = out.createInput();\r
+ this.out = out;\r
}\r
\r
@Override\r
protected void compute()\r
{\r
if (enable.getValue() == Bit.ONE)\r
- outI.feedSignals(in.getValues());\r
+ out.feedSignals(in.getValues());\r
else\r
- outI.clearSignals();\r
+ out.clearSignals();\r
}\r
\r
@Override\r
- public List<WireArray> getAllInputs()\r
+ public List<WireEnd> getAllInputs()\r
{\r
- return List.of(in, enable);\r
+ return Collections.unmodifiableList(Arrays.asList(in, enable));\r
}\r
\r
@Override\r
- public List<WireArray> getAllOutputs()\r
+ public List<WireEnd> getAllOutputs()\r
{\r
- return List.of(outI.owner);\r
+ return Collections.unmodifiableList(Arrays.asList(out));\r
}\r
\r
}\r
package era.mi.logic.components.gates;\r
\r
import era.mi.logic.Util;\r
-import era.mi.logic.wires.WireArray;\r
+import era.mi.logic.wires.Wire.WireEnd;\r
\r
public class AndGate extends MultiInputGate\r
{\r
- public AndGate(int processTime, WireArray out, WireArray... in)\r
+ public AndGate(int processTime, WireEnd out, WireEnd... in)\r
{\r
super(processTime, Util::and, out, in);\r
}\r
package era.mi.logic.components.gates;\r
\r
+import java.util.Arrays;\r
+import java.util.Collections;\r
import java.util.List;\r
\r
import era.mi.logic.Bit;\r
import era.mi.logic.components.BasicComponent;\r
-import era.mi.logic.wires.WireArray;\r
-import era.mi.logic.wires.WireArray.WireArrayEnd;\r
+import era.mi.logic.wires.Wire.WireEnd;\r
\r
public abstract class MultiInputGate extends BasicComponent\r
{\r
- protected WireArray[] in;\r
- protected WireArray out;\r
- protected WireArrayEnd outI;\r
+ protected WireEnd[] in;\r
+ protected WireEnd out;\r
protected final int length;\r
protected Operation op;\r
\r
- protected MultiInputGate(int processTime, Operation op, WireArray out, WireArray... in)\r
+ protected MultiInputGate(int processTime, Operation op, WireEnd out, WireEnd... in)\r
{\r
super(processTime);\r
this.op = op;\r
- length = out.length;\r
+ length = out.length();\r
this.in = in.clone();\r
if (in.length < 1)\r
throw new IllegalArgumentException(String.format("Cannot create gate with %d wires.", in.length));\r
- for (WireArray w : in)\r
+ for (WireEnd w : in)\r
{\r
- if (w.length != length)\r
+ if (w.length() != length)\r
throw new IllegalArgumentException("All wires connected to the gate must be of uniform length.");\r
w.addObserver(this);\r
}\r
this.out = out;\r
- outI = out.createInput();\r
}\r
\r
@Override\r
- public List<WireArray> getAllInputs()\r
+ public List<WireEnd> getAllInputs()\r
{\r
- return List.of(in);\r
+ return Collections.unmodifiableList(Arrays.asList(in));\r
}\r
\r
@Override\r
- public List<WireArray> getAllOutputs()\r
+ public List<WireEnd> getAllOutputs()\r
{\r
- return List.of(out);\r
+ return Collections.unmodifiableList(Arrays.asList(out));\r
}\r
\r
- @Override\r
protected void compute()\r
{\r
Bit[] result = in[0].getValues();\r
for (int i = 1; i < in.length; i++)\r
result = op.execute(result, in[i].getValues());\r
- outI.feedSignals(result);\r
+ out.feedSignals(result);\r
}\r
\r
protected interface Operation\r
package era.mi.logic.components.gates;\r
\r
+import java.util.Arrays;\r
+import java.util.Collections;\r
import java.util.List;\r
\r
import era.mi.logic.Util;\r
import era.mi.logic.components.BasicComponent;\r
-import era.mi.logic.wires.WireArray;\r
-import era.mi.logic.wires.WireArray.WireArrayEnd;\r
+import era.mi.logic.wires.Wire.WireEnd;\r
\r
public class NotGate extends BasicComponent\r
{\r
- private WireArray in, out;\r
- private WireArrayEnd outI;\r
+ private WireEnd in;\r
+ private WireEnd out;\r
\r
- public NotGate(int processTime, WireArray in, WireArray out)\r
+ public NotGate(int processTime, WireEnd in, WireEnd out)\r
{\r
super(processTime);\r
this.in = in;\r
in.addObserver(this);\r
this.out = out;\r
- outI = out.createInput();\r
}\r
\r
@Override\r
- public void compute()\r
+ protected void compute()\r
{\r
- outI.feedSignals(Util.not(in.getValues()));\r
+ out.feedSignals(Util.not(in.getValues()));\r
}\r
\r
- public WireArray getIn()\r
+ public WireEnd getIn()\r
{\r
return in;\r
}\r
\r
- public WireArray getOut()\r
+ public WireEnd getOut()\r
{\r
return out;\r
}\r
\r
@Override\r
- public List<WireArray> getAllInputs()\r
+ public List<WireEnd> getAllInputs()\r
{\r
- return List.of(in);\r
+ return Collections.unmodifiableList(Arrays.asList(in));\r
}\r
\r
@Override\r
- public List<WireArray> getAllOutputs()\r
+ public List<WireEnd> getAllOutputs()\r
{\r
- return List.of(out);\r
+ return Collections.unmodifiableList(Arrays.asList(out));\r
}\r
}\r
package era.mi.logic.components.gates;\r
\r
import era.mi.logic.Util;\r
-import era.mi.logic.wires.WireArray;\r
+import era.mi.logic.wires.Wire.WireEnd;\r
\r
public class OrGate extends MultiInputGate\r
{\r
- public OrGate(int processTime, WireArray out, WireArray... in)\r
+ public OrGate(int processTime, WireEnd out, WireEnd... in)\r
{\r
super(processTime, Util::or, out, in);\r
}\r
package era.mi.logic.components.gates;\r
\r
import era.mi.logic.Util;\r
-import era.mi.logic.wires.WireArray;\r
+import era.mi.logic.wires.Wire.WireEnd;\r
\r
/**\r
* Outputs 1 when the number of 1 inputs is odd.\r
*/\r
public class XorGate extends MultiInputGate\r
{\r
- public XorGate(int processTime, WireArray out, WireArray... in)\r
+ public XorGate(int processTime, WireEnd out, WireEnd... in)\r
{\r
super(processTime, Util::xor, out, in);\r
}\r
\r
import era.mi.logic.Bit;\r
import era.mi.logic.Simulation;\r
-import era.mi.logic.components.Connector;\r
import era.mi.logic.components.Demux;\r
import era.mi.logic.components.Merger;\r
import era.mi.logic.components.Mux;\r
import era.mi.logic.components.gates.NotGate;\r
import era.mi.logic.components.gates.OrGate;\r
import era.mi.logic.components.gates.XorGate;\r
-import era.mi.logic.wires.WireArray;\r
-import era.mi.logic.wires.WireArray.WireArrayEnd;\r
+import era.mi.logic.wires.Wire;\r
+import era.mi.logic.wires.Wire.WireEnd;\r
\r
-@SuppressWarnings("unused")\r
class ComponentTest\r
{\r
\r
void circuitExampleTest()\r
{\r
Simulation.TIMELINE.reset();\r
- WireArray a = new WireArray(1, 1), b = new WireArray(1, 1), c = new WireArray(1, 10), d = new WireArray(2, 1),\r
- e = new WireArray(1, 1), f = new WireArray(1, 1), g = new WireArray(1, 1), h = new WireArray(2, 1), i = new WireArray(2, 1),\r
- j = new WireArray(1, 1), k = new WireArray(1, 1);\r
- new AndGate(1, f, a, b);\r
- new NotGate(1, f, g);\r
- new Merger(h, c, g);\r
- new Mux(1, i, e, h, d);\r
- new Splitter(i, k, j);\r
-\r
- a.createInput().feedSignals(Bit.ZERO);\r
- b.createInput().feedSignals(Bit.ONE);\r
- c.createInput().feedSignals(Bit.ZERO);\r
- d.createInput().feedSignals(Bit.ONE, Bit.ONE);\r
- e.createInput().feedSignals(Bit.ZERO);\r
+ Wire a = new Wire(1, 1), b = new Wire(1, 1), c = new Wire(1, 10), d = new Wire(2, 1),\r
+ e = new Wire(1, 1), f = new Wire(1, 1), g = new Wire(1, 1), h = new Wire(2, 1), i = new Wire(2, 1),\r
+ j = new Wire(1, 1), k = new Wire(1, 1);\r
+ new AndGate(1, f.createEnd(), a.createEnd(), b.createEnd());\r
+ new NotGate(1, f.createEnd(), g.createEnd());\r
+ new Merger(h.createEnd(), c.createEnd(), g.createEnd());\r
+ new Mux(1, i.createEnd(), e.createEnd(), h.createEnd(), d.createEnd());\r
+ new Splitter(i.createEnd(), k.createEnd(), j.createEnd());\r
+\r
+ a.createEnd().feedSignals(Bit.ZERO);\r
+ b.createEnd().feedSignals(Bit.ONE);\r
+ c.createEnd().feedSignals(Bit.ZERO);\r
+ d.createEnd().feedSignals(Bit.ONE, Bit.ONE);\r
+ e.createEnd().feedSignals(Bit.ZERO);\r
\r
Simulation.TIMELINE.executeAll();\r
\r
void splitterTest()\r
{\r
Simulation.TIMELINE.reset();\r
- WireArray a = new WireArray(3, 1), b = new WireArray(2, 1), c = new WireArray(3, 1), in = new WireArray(8, 1);\r
- in.createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);\r
- new Splitter(in, a, b, c);\r
+ Wire a = new Wire(3, 1), b = new Wire(2, 1), c = new Wire(3, 1), in = new Wire(8, 1);\r
+ in.createEnd().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);\r
+ new Splitter(in.createEnd(), a.createEnd(), b.createEnd(), c.createEnd());\r
\r
Simulation.TIMELINE.executeAll();\r
\r
void mergerTest()\r
{\r
Simulation.TIMELINE.reset();\r
- WireArray a = new WireArray(3, 1), b = new WireArray(2, 1), c = new WireArray(3, 1), out = new WireArray(8, 1);\r
- a.createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO);\r
- b.createInput().feedSignals(Bit.ONE, Bit.ZERO);\r
- c.createInput().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE);\r
+ Wire a = new Wire(3, 1), b = new Wire(2, 1), c = new Wire(3, 1), out = new Wire(8, 1);\r
+ a.createEnd().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO);\r
+ b.createEnd().feedSignals(Bit.ONE, Bit.ZERO);\r
+ c.createEnd().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE);\r
\r
- new Merger(out, a, b, c);\r
+ new Merger(out.createEnd(), a.createEnd(), b.createEnd(), c.createEnd());\r
\r
Simulation.TIMELINE.executeAll();\r
\r
@Test\r
void triStateBufferTest()\r
{\r
- WireArray a = new WireArray(1, 1), b = new WireArray(1, 1), en = new WireArray(1, 1), notEn = new WireArray(1, 1);\r
- new NotGate(1, en, notEn);\r
- new TriStateBuffer(1, a, b, en);\r
- new TriStateBuffer(1, b, a, notEn);\r
+ Wire a = new Wire(1, 1), b = new Wire(1, 1), en = new Wire(1, 1), notEn = new Wire(1, 1);\r
+ new NotGate(1, en.createEnd(), notEn.createEnd());\r
+ new TriStateBuffer(1, a.createEnd(), b.createEnd(), en.createEnd());\r
+ new TriStateBuffer(1, b.createEnd(), a.createEnd(), notEn.createEnd());\r
\r
- WireArrayEnd enI = en.createInput(), aI = a.createInput(), bI = b.createInput();\r
+ WireEnd enI = en.createEnd(), aI = a.createEnd(), bI = b.createEnd();\r
enI.feedSignals(Bit.ONE);\r
aI.feedSignals(Bit.ONE);\r
- bI.feedSignals(Bit.Z);\r
\r
Simulation.TIMELINE.executeAll();\r
\r
void muxTest()\r
{\r
Simulation.TIMELINE.reset();\r
- WireArray a = new WireArray(4, 3), b = new WireArray(4, 6), c = new WireArray(4, 4), select = new WireArray(2, 5),\r
- out = new WireArray(4, 1);\r
- WireArrayEnd selectIn = select.createInput();\r
+ Wire a = new Wire(4, 3), b = new Wire(4, 6), c = new Wire(4, 4), select = new Wire(2, 5),\r
+ out = new Wire(4, 1);\r
+ WireEnd selectIn = select.createEnd();\r
\r
selectIn.feedSignals(Bit.ZERO, Bit.ZERO);\r
- a.createInput().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);\r
- c.createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);\r
+ a.createEnd().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);\r
+ c.createEnd().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);\r
\r
- new Mux(1, out, select, a, b, c);\r
+ new Mux(1, out.createEnd(), select.createEnd(), a.createEnd(), b.createEnd(), c.createEnd());\r
Simulation.TIMELINE.executeAll();\r
\r
assertBitArrayEquals(out.getValues(), Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);\r
void demuxTest()\r
{\r
Simulation.TIMELINE.reset();\r
- WireArray a = new WireArray(4, 3), b = new WireArray(4, 6), c = new WireArray(4, 4), select = new WireArray(2, 5),\r
- in = new WireArray(4, 1);\r
- WireArrayEnd selectIn = select.createInput();\r
+ Wire a = new Wire(4, 3), b = new Wire(4, 6), c = new Wire(4, 4), select = new Wire(2, 5),\r
+ in = new Wire(4, 1);\r
+ WireEnd selectIn = select.createEnd();\r
\r
selectIn.feedSignals(Bit.ZERO, Bit.ZERO);\r
- in.createInput().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);\r
+ in.createEnd().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);\r
\r
- new Demux(1, in, select, a, b, c);\r
+ new Demux(1, in.createEnd(), select.createEnd(), a.createEnd(), b.createEnd(), c.createEnd());\r
Simulation.TIMELINE.executeAll();\r
\r
assertBitArrayEquals(a.getValues(), Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);\r
void andTest()\r
{\r
Simulation.TIMELINE.reset();\r
- WireArray a = new WireArray(4, 1), b = new WireArray(4, 3), c = new WireArray(4, 1);\r
- new AndGate(1, c, a, b);\r
- a.createInput().feedSignals(Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ZERO);\r
- b.createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);\r
+ Wire a = new Wire(4, 1), b = new Wire(4, 3), c = new Wire(4, 1);\r
+ new AndGate(1, c.createEnd(), a.createEnd(), b.createEnd());\r
+ a.createEnd().feedSignals(Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ZERO);\r
+ b.createEnd().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);\r
\r
Simulation.TIMELINE.executeAll();\r
\r
void orTest()\r
{\r
Simulation.TIMELINE.reset();\r
- WireArray a = new WireArray(4, 1), b = new WireArray(4, 3), c = new WireArray(4, 1);\r
- new OrGate(1, c, a, b);\r
- a.createInput().feedSignals(Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ZERO);\r
- b.createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);\r
+ Wire a = new Wire(4, 1), b = new Wire(4, 3), c = new Wire(4, 1);\r
+ new OrGate(1, c.createEnd(), a.createEnd(), b.createEnd());\r
+ a.createEnd().feedSignals(Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ZERO);\r
+ b.createEnd().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);\r
\r
Simulation.TIMELINE.executeAll();\r
\r
void xorTest()\r
{\r
Simulation.TIMELINE.reset();\r
- WireArray a = new WireArray(3, 1), b = new WireArray(3, 2), c = new WireArray(3, 1), d = new WireArray(3, 1);\r
- new XorGate(1, d, a, b, c);\r
- a.createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ONE);\r
- b.createInput().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE);\r
- c.createInput().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE);\r
+ Wire a = new Wire(3, 1), b = new Wire(3, 2), c = new Wire(3, 1), d = new Wire(3, 1);\r
+ new XorGate(1, d.createEnd(), a.createEnd(), b.createEnd(), c.createEnd());\r
+ a.createEnd().feedSignals(Bit.ZERO, Bit.ONE, Bit.ONE);\r
+ b.createEnd().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE);\r
+ c.createEnd().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE);\r
\r
Simulation.TIMELINE.executeAll();\r
\r
assertBitArrayEquals(d.getValues(), Bit.ZERO, Bit.ONE, Bit.ONE);\r
}\r
+ \r
+ @Test\r
+ void notTest()\r
+ {\r
+ Simulation.TIMELINE.reset();\r
+ Wire a = new Wire(3, 1), b = new Wire(3, 2);\r
+ new NotGate(1, a.createEnd(), b.createEnd());\r
+ a.createEnd().feedSignals(Bit.ZERO, Bit.ONE, Bit.ONE);\r
+\r
+ Simulation.TIMELINE.executeAll();\r
+\r
+ assertBitArrayEquals(b.getValues(), Bit.ONE, Bit.ZERO, Bit.ZERO);\r
+ }\r
\r
@Test\r
void rsLatchCircuitTest()\r
{\r
Simulation.TIMELINE.reset();\r
- WireArray r = new WireArray(1, 1), s = new WireArray(1, 1), t1 = new WireArray(1, 15), t2 = new WireArray(1, 1),\r
- q = new WireArray(1, 1), nq = new WireArray(1, 1);\r
+ Wire r = new Wire(1, 1), s = new Wire(1, 1), t1 = new Wire(1, 15), t2 = new Wire(1, 1),\r
+ q = new Wire(1, 1), nq = new Wire(1, 1);\r
\r
- new OrGate(1, t2, r, nq);\r
- new OrGate(1, t1, s, q);\r
- new NotGate(1, t2, q);\r
- new NotGate(1, t1, nq);\r
+ new OrGate(1, t2.createEnd(), r.createEnd(), nq.createEnd());\r
+ new OrGate(1, t1.createEnd(), s.createEnd(), q.createEnd());\r
+ new NotGate(1, t2.createEnd(), q.createEnd());\r
+ new NotGate(1, t1.createEnd(), nq.createEnd());\r
\r
- WireArrayEnd sIn = s.createInput(), rIn = r.createInput();\r
+ WireEnd sIn = s.createEnd(), rIn = r.createEnd();\r
\r
sIn.feedSignals(Bit.ONE);\r
rIn.feedSignals(Bit.ZERO);\r
{\r
Simulation.TIMELINE.reset();\r
\r
- WireArray a = new WireArray(4, 1);\r
- a.createInput().feedSignals(Bit.ONE, Bit.ONE, Bit.ONE, Bit.ONE);\r
+ Wire a = new Wire(4, 1);\r
+ a.createEnd().feedSignals(Bit.ONE, Bit.ONE, Bit.ONE, Bit.ONE);\r
\r
Simulation.TIMELINE.executeAll();\r
\r
void multipleInputs()\r
{\r
Simulation.TIMELINE.reset();\r
- WireArray w = new WireArray(2, 1);\r
- WireArrayEnd wI1 = w.createInput(), wI2 = w.createInput();\r
+ Wire w = new Wire(2, 1);\r
+ WireEnd wI1 = w.createEnd(), wI2 = w.createEnd();\r
wI1.feedSignals(Bit.ONE, Bit.Z);\r
wI2.feedSignals(Bit.Z, Bit.X);\r
Simulation.TIMELINE.executeAll();\r
assertBitArrayEquals(w.getValues(), Bit.ONE, Bit.Z);\r
}\r
\r
- @Test\r
+// @Test\r
void wireConnections()\r
{\r
// Nur ein Experiment, was über mehrere 'passive' Bausteine hinweg passieren würde\r
\r
Simulation.TIMELINE.reset();\r
\r
- WireArray a = new WireArray(1, 2);\r
- WireArray b = new WireArray(1, 2);\r
- WireArray c = new WireArray(1, 2);\r
- WireArrayEnd aI = a.createInput();\r
- WireArrayEnd bI = b.createInput();\r
- WireArrayEnd cI = c.createInput();\r
+ Wire a = new Wire(1, 2);\r
+ Wire b = new Wire(1, 2);\r
+ Wire c = new Wire(1, 2);\r
+ WireEnd aI = a.createEnd();\r
+ WireEnd bI = b.createEnd();\r
+ WireEnd cI = c.createEnd();\r
\r
- TestBitDisplay test = new TestBitDisplay(c);\r
- TestBitDisplay test2 = new TestBitDisplay(a);\r
+ TestBitDisplay test = new TestBitDisplay(c.createEnd());\r
+ TestBitDisplay test2 = new TestBitDisplay(a.createEnd());\r
LongConsumer print = time -> System.out.format("Time %2d\n a: %s\n b: %s\n c: %s\n", time, a, b, c);\r
\r
cI.feedSignals(Bit.ONE);\r
cI.feedSignals(Bit.Z);\r
test.assertAfterSimulationIs(print, Bit.Z);\r
\r
- new Connector(b, c).connect();\r
+ new Connector(b, c);\r
test.assertAfterSimulationIs(print, Bit.Z);\r
System.err.println("ONE");\r
bI.feedSignals(Bit.ONE);\r
bI.feedSignals(Bit.Z);\r
test.assertAfterSimulationIs(print, Bit.Z);\r
\r
- new Connector(a, b).connect();\r
+ new Connector(a, b);\r
System.err.println("Z 2");\r
aI.feedSignals(Bit.Z);\r
test.assertAfterSimulationIs(print, Bit.Z);\r
--- /dev/null
+package era.mi.logic.tests;
+
+import era.mi.logic.Bit;
+import era.mi.logic.Simulation;
+import era.mi.logic.wires.Wire;
+import era.mi.logic.wires.Wire.WireEnd;
+import era.mi.logic.wires.WireArrayObserver;
+
+public class Connector implements WireArrayObserver
+{
+ private final Wire a;
+// private final WireArray b;
+ private final WireEnd aI;
+ private final WireEnd bI;
+
+ public Connector(Wire a, Wire b)
+ {
+ if (a.length != b.length)
+ throw new IllegalArgumentException(String.format("WireArray width does not match: %d, %d", a.length, b.length));
+ this.a = a;
+// this.b = b;
+ a.addObserver(this);
+ b.addObserver(this);
+ aI = a.createEnd();
+ bI = b.createEnd();
+ }
+
+ @Override
+ public void update(Wire initiator, Bit[] oldValues)
+ {
+ Simulation.TIMELINE.addEvent((e) ->
+ {
+ if (initiator == a)
+ bI.feedSignals(aI.wireValuesExcludingMe());
+ else
+ aI.feedSignals(bI.wireValuesExcludingMe());
+ }, 1);
+ }
+}
import era.mi.logic.components.gates.NotGate;\r
import era.mi.logic.components.gates.OrGate;\r
import era.mi.logic.timeline.Timeline.ExecutionResult;\r
-import era.mi.logic.wires.WireArray;\r
+import era.mi.logic.wires.Wire;\r
\r
public class GUITest extends JPanel\r
{\r
private static final int OR_DELAY = 100;\r
private static final int NOT_DELAY = 100;\r
\r
- WireArray r = new WireArray(1, WIRE_DELAY);\r
- WireArray s = new WireArray(1, WIRE_DELAY);\r
- WireArray t1 = new WireArray(1, WIRE_DELAY);\r
- WireArray t2 = new WireArray(1, WIRE_DELAY);\r
- WireArray q = new WireArray(1, WIRE_DELAY);\r
- WireArray nq = new WireArray(1, WIRE_DELAY);\r
+ Wire r = new Wire(1, WIRE_DELAY);\r
+ Wire s = new Wire(1, WIRE_DELAY);\r
+ Wire t1 = new Wire(1, WIRE_DELAY);\r
+ Wire t2 = new Wire(1, WIRE_DELAY);\r
+ Wire q = new Wire(1, WIRE_DELAY);\r
+ Wire nq = new Wire(1, WIRE_DELAY);\r
\r
- ManualSwitch rIn = new ManualSwitch(r);\r
- ManualSwitch sIn = new ManualSwitch(s);\r
+ ManualSwitch rIn = new ManualSwitch(r.createEnd());\r
+ ManualSwitch sIn = new ManualSwitch(s.createEnd());\r
\r
- OrGate or1 = new OrGate(OR_DELAY, t2, r, nq);\r
- OrGate or2 = new OrGate(OR_DELAY, t1, s, q);\r
- NotGate not1 = new NotGate(NOT_DELAY, t2, q);\r
- NotGate not2 = new NotGate(NOT_DELAY, t1, nq);\r
+ OrGate or1 = new OrGate(OR_DELAY, t2.createEnd(), r.createEnd(), nq.createEnd());\r
+ OrGate or2 = new OrGate(OR_DELAY, t1.createEnd(), s.createEnd(), q.createEnd());\r
+ NotGate not1 = new NotGate(NOT_DELAY, t2.createEnd(), q.createEnd());\r
+ NotGate not2 = new NotGate(NOT_DELAY, t1.createEnd(), nq.createEnd());\r
\r
Map<ManualSwitch, Rectangle> switchMap = new HashMap<>();\r
\r
g.setFont(g.getFont().deriveFont(Math.min(height, width) / 40f));\r
}\r
\r
- private static void drawString(Graphics g, String s, int x, int y, double anchorX, double anchorY)\r
+ private void drawString(Graphics g, String s, int x, int y, double anchorX, double anchorY)\r
{\r
int h = g.getFontMetrics().getAscent();\r
int w = g.getFontMetrics().stringWidth(s);\r
g.drawString(s, x - (int) (w * anchorX), y + (int) (h * anchorY));\r
}\r
\r
- private void drawWire(Graphics g, WireArray wa, String name, double x1, double y1, double x2, double y2)\r
+ private void drawWire(Graphics g, Wire wa, String name, double x1, double y1, double x2, double y2)\r
{\r
setTo(g, wa);\r
g.drawLine(gX(x1), gY(y1), gX(x2), gY(y2));\r
g.setColor(Color.BLACK);\r
}\r
\r
- private static void setTo(Graphics g, WireArray wa)\r
+ private static void setTo(Graphics g, Wire wa)\r
{\r
switch (wa.getValue())\r
{\r
g.setColor(Color.BLACK);\r
break;\r
case U:\r
- g.setColor(Color.MAGENTA);\r
+ g.setColor(Color.BLUE);\r
break;\r
default:\r
throw new IllegalArgumentException();\r
gt.repaint(12);\r
try\r
{\r
- Thread.sleep(Math.max(updateT - System.currentTimeMillis() + lastFrame, 0));\r
+ Thread.sleep(Math.max(16 - System.currentTimeMillis() + lastFrame, 0));\r
}\r
catch (Exception e)\r
{\r
import era.mi.logic.Bit;\r
import era.mi.logic.Simulation;\r
import era.mi.logic.components.BitDisplay;\r
-import era.mi.logic.wires.WireArray;\r
+import era.mi.logic.wires.Wire.WireEnd;\r
\r
public final class TestBitDisplay extends BitDisplay\r
{\r
\r
- public TestBitDisplay(WireArray in)\r
+ public TestBitDisplay(WireEnd in)\r
{\r
super(in);\r
}\r
{\r
if (!hasNext())\r
return -1;\r
- return events.peek().timing;\r
+ else\r
+ return events.peek().timing;\r
}\r
\r
public void reset()\r
private class InnerEvent\r
{\r
\r
- final long timing;\r
+ private final long timing;\r
private final TimelineEventHandler function;\r
private final TimelineEvent event;\r
\r
return timing;\r
}\r
\r
- @Override\r
public String toString()\r
{\r
return "timestamp: " + timing;\r
--- /dev/null
+package era.mi.logic.wires;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.List;
+
+import era.mi.logic.Bit;
+import era.mi.logic.Simulation;
+import era.mi.logic.Util;
+
+/**
+ * Represents an array of wires that can store n bits of information.
+ *
+ * @author Fabian Stemmler
+ *
+ */
+public class Wire
+{
+ private Bit[] values;
+ public final int travelTime;
+ private List<WireArrayObserver> observers = new ArrayList<WireArrayObserver>();
+ public final int length;
+ private List<WireEnd> inputs = new ArrayList<WireEnd>();
+
+ public Wire(int length, int travelTime)
+ {
+ if (length < 1)
+ throw new IllegalArgumentException(
+ String.format("Tried to create an array of wires with length %d, but a length of less than 1 makes no sense.", length));
+ this.length = length;
+ this.travelTime = travelTime;
+ initValues();
+ }
+
+ private void initValues()
+ {
+ values = Bit.U.makeArray(length);
+ }
+
+ private void recalculateSingleInput()
+ {
+ WireEnd input = inputs.get(0);
+ if (!Arrays.equals(input.getInputValues(), values))
+ {
+ Bit[] oldValues = values.clone();
+ System.arraycopy(input.getInputValues(), 0, values, 0, length);
+ notifyObservers(oldValues);
+ }
+ }
+
+ private void recalculateMultipleInputs()
+ {
+ Iterator<WireEnd> it = inputs.iterator();
+ Bit[] newValues = it.next().inputValues.clone();
+
+ while (it.hasNext())
+ {
+ WireEnd input = it.next();
+ Bit[] bits = input.getInputValues();
+ for (int i = 0; i < length; i++)
+ {
+ if (Bit.Z.equals(bits[i]) || newValues[i].equals(bits[i]))
+ continue;
+ else if (Bit.Z.equals(newValues[i]))
+ newValues[i] = bits[i];
+ else
+ newValues[i] = Bit.X;
+ }
+ }
+
+ if (!Arrays.equals(newValues, values))
+ {
+ Bit[] oldValues = values;
+ values = newValues;
+ notifyObservers(oldValues);
+ }
+ }
+
+ private void recalculate()
+ {
+ switch (inputs.size())
+ {
+ case 0:
+ return;
+ case 1:
+ recalculateSingleInput();
+ break;
+ default:
+ recalculateMultipleInputs();
+ }
+ }
+
+ /**
+ * The {@link Wire} is interpreted as an unsigned integer with n bits.
+ *
+ * @return <code>true</code> if all bits are either <code>Bit.ONE</code> or <code>Bit.ZERO</code> (they do not all have to have the same
+ * value), not <code>Bit.X</code> or <code>Bit.Z</code>. <code>false</code> is returned otherwise.
+ *
+ * @author Fabian Stemmler
+ */
+ public boolean hasNumericValue()
+ {
+ for (Bit b : values)
+ {
+ if (b != Bit.ZERO && b != Bit.ONE)
+ return false;
+ }
+ return true;
+ }
+
+ /**
+ * The {@link Wire} is interpreted as an unsigned integer with n bits.
+ *
+ * @return The unsigned value of the {@link Wire}'s bits, where value 0 corresponds with 2^0, value 1 is 2^1 and so on.
+ *
+ * @author Fabian Stemmler
+ */
+ public long getUnsignedValue()
+ {
+ long val = 0;
+ long mask = 1;
+ for (int i = 0; i < length; i++)
+ {
+ switch (values[i])
+ {
+ default:
+ case Z:
+ case X:
+ return 0; // TODO: Proper handling for getUnsignedValue(), if not all bits are 1 or 0;
+ // Random number?
+ case ONE:
+ val |= mask;
+ break;
+ case ZERO:
+ }
+ mask = mask << 1;
+ }
+ return val;
+ }
+
+ /**
+ * The {@link Wire} is interpreted as a signed integer with n bits.
+ *
+ * @return The signed value of the {@link Wire}'s bits, where value 0 corresponds with 2^0, value 1 is 2^1 and so on.
+ *
+ * @author Fabian Stemmler
+ */
+ public long getSignedValue()
+ {
+ long val = getUnsignedValue();
+ long mask = 1 << (length - 1);
+ if ((mask & val) != 0)
+ {
+ int shifts = 64 - length;
+ return (val << shifts) >> shifts;
+ }
+ return val;
+ }
+
+ public Bit getValue()
+ {
+ return getValue(0);
+ }
+
+ public Bit getValue(int index)
+ {
+ return values[index];
+ }
+
+ public Bit[] getValues(int start, int end)
+ {
+ int length = end - start;
+ Bit[] bits = new Bit[length];
+ System.arraycopy(values, start, bits, 0, length);
+ return bits;
+ }
+
+ public Bit[] getValues()
+ {
+ return values.clone();
+ }
+
+ /**
+ * Adds an {@link WireArrayObserver}, who will be notified when the value of the {@link Wire} is updated.
+ *
+ * @param ob The {@link WireArrayObserver} to be notified of changes.
+ * @return true if the given {@link WireArrayObserver} was not already registered, false otherwise
+ *
+ * @author Fabian Stemmler
+ */
+ public boolean addObserver(WireArrayObserver ob)
+ {
+ return observers.add(ob);
+ }
+
+ private void notifyObservers(Bit[] oldValues)
+ {
+ for (WireArrayObserver o : observers)
+ o.update(this, oldValues);
+ }
+
+ /**
+ * Create and register a {@link WireEnd} object, which is tied to this {@link Wire}.
+ */
+ public WireEnd createEnd()
+ {
+ return new WireEnd();
+ }
+
+ private void registerInput(WireEnd toRegister)
+ {
+ inputs.add(toRegister);
+ }
+
+ /**
+ * A {@link WireEnd} feeds a constant signal into the {@link Wire} it is tied to. The combination of all inputs determines the
+ * {@link Wire}s final value. X dominates all other inputs Z does not affect the final value, unless there are no other inputs than
+ * Z 0 and 1 turn into X when they are mixed
+ *
+ * @author Fabian Stemmler
+ */
+ public class WireEnd
+ {
+ private boolean open;
+ private Bit[] inputValues;
+
+ private WireEnd()
+ {
+ super();
+ open = true;
+ initValues();
+ registerInput(this);
+ }
+
+ private void initValues()
+ {
+ inputValues = Bit.Z.makeArray(length);
+ }
+
+ /**
+ * Sets the wires values. This takes up time, as specified by the {@link Wire}s travel time.
+ *
+ * @param newValues The new values the wires should take on.
+ *
+ * @author Fabian Stemmler
+ */
+ public void feedSignals(Bit... newValues)
+ {
+ if (newValues.length == length)
+ {
+ feedSignals(0, newValues);
+ } else
+ throw new IllegalArgumentException(
+ String.format("Attempted to input %d bits instead of %d bits.", newValues.length, length));
+ }
+
+ /**
+ * Sets values of a subarray of wires. This takes up time, as specified by the {@link Wire}s travel time.
+ *
+ * @param newValues The new values the wires should take on.
+ * @param startingBit The first index of the subarray of wires.
+ *
+ * @author Fabian Stemmler
+ */
+ public void feedSignals(int startingBit, Bit... newValues)
+ {
+ if (!open)
+ throw new RuntimeException("Attempted to write to closed WireArrayEnd.");
+ Simulation.TIMELINE.addEvent((e) -> setValues(startingBit, newValues), travelTime);
+ }
+
+ private void setValues(int startingBit, Bit... newValues)
+ {
+ int exclLastIndex = startingBit + newValues.length;
+ if (length < exclLastIndex)
+ throw new ArrayIndexOutOfBoundsException(
+ String.format("Attempted to input bits from index %d to %d when there are only %d wires.", startingBit,
+ exclLastIndex - 1, length));
+ if (!Arrays.equals(inputValues, startingBit, exclLastIndex, newValues, 0, newValues.length))
+ {
+ System.arraycopy(newValues, 0, inputValues, startingBit, newValues.length);
+ Wire.this.recalculate();
+ }
+ }
+
+ /**
+ * @return The value (of bit 0) the {@link WireEnd} is currently feeding into the associated {@link Wire}.
+ */
+ public Bit getInputValue()
+ {
+ return getInputValue(0);
+ }
+
+ /**
+ * @return The value which the {@link WireEnd} is currently feeding into the associated {@link Wire} at the indexed {@link Bit}.
+ */
+ public Bit getInputValue(int index)
+ {
+ return inputValues[index];
+ }
+
+ /**
+ * @return A copy (safe to modify) of the values the {@link WireEnd} is currently feeding into the associated
+ * {@link Wire}.
+ */
+ public Bit[] getInputValues()
+ {
+ return getInputValues(0, length);
+ }
+
+ public Bit[] getInputValues(int start, int end)
+ {
+ int length = end - start;
+ Bit[] bits = new Bit[length];
+ System.arraycopy(inputValues, start, bits, 0, length);
+ return bits;
+ }
+
+ /**
+ * {@link WireEnd} now feeds Z into the associated {@link Wire}.
+ */
+ public void clearSignals()
+ {
+ feedSignals(Bit.Z.makeArray(length));
+ }
+
+ public Bit[] wireValuesExcludingMe()
+ {
+ Bit[] bits = Bit.Z.makeArray(length);
+ for (WireEnd wai : inputs)
+ {
+ if (wai == this)
+ continue;
+ Util.combineInto(bits, wai.getInputValues());
+ }
+ return bits;
+ }
+
+ /**
+ * Included for convenient use on {@link Wire}s of length 1.
+ *
+ * @return The value of bit 0.
+ *
+ * @author Fabian Stemmler
+ */
+ public Bit getValue()
+ {
+ return Wire.this.getValue();
+ }
+
+ /**
+ * @param index Index of the requested bit.
+ * @return The value of the indexed bit.
+ *
+ * @author Fabian Stemmler
+ */
+ public Bit getValue(int index)
+ {
+ return Wire.this.getValue(index);
+ }
+
+ /**
+ * @param index Index of the requested bit.
+ * @return The value of the indexed bit.
+ *
+ * @author Fabian Stemmler
+ */
+ public Bit[] getValues()
+ {
+ return Wire.this.getValues();
+ }
+
+ /**
+ * @param start Start of the wanted segment. (inclusive)
+ * @param end End of the wanted segment. (exclusive)
+ * @return The values of the segment of {@link Bit}s indexed.
+ *
+ * @author Fabian Stemmler
+ */
+ public Bit[] getValues(int start, int end)
+ {
+ return Wire.this.getValues(start, end);
+ }
+
+
+ /**
+ * The {@link Wire} is interpreted as an unsigned integer with n bits.
+ *
+ * @return <code>true</code> if all bits are either <code>Bit.ONE</code> or <code>Bit.ZERO</code> (they do not all have to have the same
+ * value), not <code>Bit.X</code> or <code>Bit.Z</code>. <code>false</code> is returned otherwise.
+ *
+ * @author Fabian Stemmler
+ */
+ public boolean hasNumericValue()
+ {
+ return Wire.this.hasNumericValue();
+ }
+
+ /**
+ * The {@link Wire} is interpreted as an unsigned integer with n bits.
+ *
+ * @return The unsigned value of the {@link Wire}'s bits, where value 0 corresponds with 2^0, value 1 is 2^1 and so on.
+ *
+ * @author Fabian Stemmler
+ */
+ public long getUnsignedValue()
+ {
+ return Wire.this.getUnsignedValue();
+ }
+
+ /**
+ * The {@link Wire} is interpreted as a signed integer with n bits.
+ *
+ * @return The signed value of the {@link Wire}'s bits, where value 0 corresponds with 2^0, value 1 is 2^1 and so on.
+ *
+ * @author Fabian Stemmler
+ */
+ public long getSignedValue()
+ {
+ return Wire.this.getSignedValue();
+ }
+
+ @Override
+ public String toString()
+ {
+ return Arrays.toString(values);
+ //return String.format("%s \nFeeding: %s", WireArray.this.toString(), Arrays.toString(inputValues));
+ }
+
+ public void disconnect()
+ {
+ inputs.remove(this);
+ open = false;
+ }
+
+ public int length()
+ {
+ return length;
+ }
+
+ public boolean addObserver(WireArrayObserver ob)
+ {
+ return Wire.this.addObserver(ob);
+ }
+
+ public Wire getWire()
+ {
+ return Wire.this;
+ }
+ }
+
+ @Override
+ public String toString()
+ {
+ return String.format("wire 0x%08x value: %s inputs: %s", hashCode(), Arrays.toString(values), inputs);
+ //Arrays.toString(values), inputs.stream().map(i -> Arrays.toString(i.inputValues)).reduce((s1, s2) -> s1 + s2)
+ }
+
+ public static WireEnd[] extractEnds(Wire[] w)
+ {
+ WireEnd[] inputs = new WireEnd[w.length];
+ for (int i = 0; i < w.length; i++)
+ inputs[i] = w[i].createEnd();
+ return inputs;
+ }
+}
\ No newline at end of file
+++ /dev/null
-package era.mi.logic.wires;\r
-\r
-import java.io.Closeable;\r
-import java.util.ArrayList;\r
-import java.util.Arrays;\r
-import java.util.Iterator;\r
-import java.util.List;\r
-\r
-import era.mi.logic.Bit;\r
-import era.mi.logic.Simulation;\r
-import era.mi.logic.Util;\r
-\r
-/**\r
- * Represents an array of wires that can store n bits of information.\r
- * \r
- * @author Fabian Stemmler\r
- *\r
- */\r
-public class WireArray\r
-{\r
- private Bit[] values;\r
- public final int travelTime;\r
- private List<WireArrayObserver> observers = new ArrayList<WireArrayObserver>();\r
- public final int length;\r
- List<WireArrayEnd> inputs = new ArrayList<WireArrayEnd>();\r
-\r
- public WireArray(int length, int travelTime)\r
- {\r
- if (length < 1)\r
- throw new IllegalArgumentException(\r
- String.format("Tried to create an array of wires with length %d, but a length of less than 1 makes no sense.", length));\r
- this.length = length;\r
- this.travelTime = travelTime;\r
- initValues();\r
- }\r
-\r
- private void initValues()\r
- {\r
- values = Bit.U.makeArray(length);\r
- }\r
-\r
- private void recalculateSingleInput()\r
- {\r
- WireArrayEnd input = inputs.get(0);\r
- if (!Arrays.equals(input.getValues(), values))\r
- {\r
- Bit[] oldValues = values.clone();\r
- System.arraycopy(input.getValues(), 0, values, 0, length);\r
- notifyObservers(oldValues);\r
- }\r
- }\r
-\r
- private void recalculateMultipleInputs()\r
- {\r
- Iterator<WireArrayEnd> it = inputs.iterator();\r
- Bit[] newValues = it.next().inputValues.clone();\r
-\r
- while (it.hasNext())\r
- {\r
- WireArrayEnd input = it.next();\r
- Bit[] bits = input.getValues();\r
- for (int i = 0; i < length; i++)\r
- {\r
- newValues[i] = newValues[i].combineWith(bits[i]);\r
- }\r
- }\r
-\r
- if (!Arrays.equals(newValues, values))\r
- {\r
- Bit[] oldValues = values;\r
- values = newValues;\r
- notifyObservers(oldValues);\r
- }\r
- }\r
-\r
- void recalculate()\r
- {\r
- switch (inputs.size())\r
- {\r
- case 0:\r
- return;\r
- case 1:\r
- recalculateSingleInput();\r
- break;\r
- default:\r
- recalculateMultipleInputs();\r
- }\r
- }\r
-\r
- /**\r
- * The WireArray is interpreted as an unsigned integer with n bits.\r
- * \r
- * @return <code>true</code> if all bits are either <code>Bit.ONE</code> or <code>Bit.ZERO</code> (they do not all have to have the same\r
- * value), not <code>Bit.X</code> or <code>Bit.Z</code>. <code>false</code> is returned otherwise.\r
- * \r
- * @author Fabian Stemmler\r
- */\r
- public boolean hasNumericValue()\r
- {\r
- for (Bit b : values)\r
- {\r
- if (b != Bit.ZERO && b != Bit.ONE)\r
- return false;\r
- }\r
- return true;\r
- }\r
-\r
- /**\r
- * The WireArray is interpreted as an unsigned integer with n bits.\r
- * \r
- * @return The unsigned value of the {@link WireArray}'s bits, where value 0 corresponds with 2^0, value 1 is 2^1 and so on.\r
- * \r
- * @author Fabian Stemmler\r
- */\r
- public long getUnsignedValue()\r
- {\r
- long val = 0;\r
- long mask = 1;\r
- for (int i = 0; i < length; i++)\r
- {\r
- switch (values[i])\r
- {\r
- default:\r
- case Z:\r
- case X:\r
- return 0; // TODO: Proper handling for getUnsignedValue(), if not all bits are 1 or 0;\r
- // Random number?\r
- case ONE:\r
- val |= mask;\r
- break;\r
- case ZERO:\r
- }\r
- mask = mask << 1;\r
- }\r
- return val;\r
- }\r
-\r
- /**\r
- * The WireArray is interpreted as a signed integer with n bits.\r
- * \r
- * @return The signed value of the {@link WireArray}'s bits, where value 0 corresponds with 2^0, value 1 is 2^1 and so on.\r
- * \r
- * @author Fabian Stemmler\r
- */\r
- public long getSignedValue()\r
- {\r
- long val = getUnsignedValue();\r
- long mask = 1 << (length - 1);\r
- if ((mask & val) != 0)\r
- {\r
- int shifts = 64 - length;\r
- return (val << shifts) >> shifts;\r
- }\r
- return val;\r
- }\r
-\r
- /**\r
- * Included for convenient use on {@link WireArray}s of length 1.\r
- * \r
- * @return The value of bit 0.\r
- * \r
- * @author Fabian Stemmler\r
- */\r
- public Bit getValue()\r
- {\r
- return getValue(0);\r
- }\r
-\r
- /**\r
- * \r
- * @param index Index of the requested bit.\r
- * @return The value of the indexed bit.\r
- * \r
- * @author Fabian Stemmler\r
- */\r
- public Bit getValue(int index)\r
- {\r
- return values[index];\r
- }\r
-\r
- public Bit[] getValues(int start, int end)\r
- {\r
- int length = end - start;\r
- Bit[] bits = new Bit[length];\r
- System.arraycopy(values, start, bits, 0, length);\r
- return bits;\r
- }\r
-\r
- /**\r
- * @return An array of length n containing the values of the n bits in the {@link WireArray}. Can be safely modified.\r
- * \r
- * @author Fabian Stemmler\r
- */\r
- public Bit[] getValues()\r
- {\r
- return values.clone();\r
- }\r
-\r
- /**\r
- * Adds an {@link WireArrayObserver}, who will be notified when the value of the {@link WireArray} is updated.\r
- * \r
- * @param ob The {@link WireArrayObserver} to be notified of changes.\r
- * @return true if the given {@link WireArrayObserver} was not already registered, false otherwise\r
- * \r
- * @author Fabian Stemmler\r
- */\r
- public boolean addObserver(WireArrayObserver ob)\r
- {\r
- return observers.add(ob);\r
- }\r
-\r
- private void notifyObservers(Bit[] oldValues)\r
- {\r
- for (WireArrayObserver o : observers)\r
- o.update(this, oldValues);\r
- }\r
-\r
- /**\r
- * Create and register a {@link WireArrayEnd} object, which is tied to this {@link WireArray}.\r
- */\r
- public WireArrayEnd createInput()\r
- {\r
- return new WireArrayEnd(this);\r
- }\r
-\r
- void registerInput(WireArrayEnd toRegister)\r
- {\r
- inputs.add(toRegister);\r
- }\r
-\r
- /**\r
- * A {@link WireArrayEnd} feeds a constant signal into the {@link WireArray} it is tied to. The combination of all inputs determines the\r
- * {@link WireArray}s final value. X dominates all other inputs Z does not affect the final value, unless there are no other inputs than\r
- * Z 0 and 1 turn into X when they are mixed\r
- * \r
- * @author Fabian Stemmler\r
- */\r
- public class WireArrayEnd implements Closeable\r
- {\r
- public final WireArray owner;\r
- private boolean open;\r
- Bit[] inputValues;\r
-\r
- WireArrayEnd(WireArray owner)\r
- {\r
- super();\r
- this.owner = owner;\r
- open = true;\r
- initValues();\r
- owner.registerInput(this);\r
- }\r
-\r
- private void initValues()\r
- {\r
- inputValues = Bit.U.makeArray(length);\r
- }\r
-\r
- /**\r
- * Sets the wires values. This takes up time, as specified by the {@link WireArray}s travel time.\r
- * \r
- * @param newValues The new values the wires should take on.\r
- * \r
- * @author Fabian Stemmler\r
- */\r
- public void feedSignals(Bit... newValues)\r
- {\r
- if (newValues.length != length)\r
- throw new IllegalArgumentException(\r
- String.format("Attempted to input %d bits instead of %d bits.", newValues.length, length));\r
- feedSignals(0, newValues);\r
-\r
- }\r
-\r
- /**\r
- * Sets values of a subarray of wires. This takes up time, as specified by the {@link WireArray}s travel time.\r
- * \r
- * @param newValues The new values the wires should take on.\r
- * @param startingBit The first index of the subarray of wires.\r
- * \r
- * @author Fabian Stemmler\r
- */\r
- public void feedSignals(int startingBit, Bit... newValues)\r
- {\r
- if (!open)\r
- throw new RuntimeException("Attempted to write to closed WireArrayEnd.");\r
- Simulation.TIMELINE.addEvent((e) -> setValues(startingBit, newValues), travelTime);\r
- }\r
-\r
- private void setValues(int startingBit, Bit... newValues)\r
- {\r
- int exclLastIndex = startingBit + newValues.length;\r
- if (length < exclLastIndex)\r
- throw new ArrayIndexOutOfBoundsException(\r
- String.format("Attempted to input bits from index %d to %d when there are only %d wires.", startingBit,\r
- exclLastIndex - 1, length));\r
- if (!Arrays.equals(inputValues, startingBit, exclLastIndex, newValues, 0, newValues.length))\r
- {\r
- System.arraycopy(newValues, 0, inputValues, startingBit, newValues.length);\r
- owner.recalculate();\r
- }\r
- }\r
-\r
- /**\r
- * Returns a copy (safe to modify) of the values the {@link WireArrayEnd} is currently feeding into the associated\r
- * {@link WireArray}.\r
- */\r
- public Bit[] getValues()\r
- {\r
- return inputValues.clone();\r
- }\r
-\r
- /**\r
- * {@link WireArrayEnd} now feeds Z into the associated {@link WireArray}.\r
- */\r
- public void clearSignals()\r
- {\r
- feedSignals(Bit.Z.makeArray(length));\r
- }\r
-\r
- public Bit[] wireValuesExcludingMe()\r
- {\r
- Bit[] bits = Bit.Z.makeArray(length);\r
- for (WireArrayEnd wai : inputs)\r
- {\r
- if (wai == this)\r
- continue;\r
- Util.combineInto(bits, wai.getValues());\r
- }\r
- return bits;\r
- }\r
-\r
- public Bit getWireValue()\r
- {\r
- return owner.getValue();\r
- }\r
-\r
- public Bit[] getWireValues()\r
- {\r
- return owner.getValues();\r
- }\r
-\r
- @Override\r
- public String toString()\r
- {\r
- return Arrays.toString(inputValues);\r
- }\r
-\r
- @Override\r
- public void close()\r
- {\r
- inputs.remove(this);\r
- open = false;\r
- }\r
- }\r
-\r
- @Override\r
- public String toString()\r
- {\r
- return String.format("wire 0x%08x value: %s inputs: %s", hashCode(), Arrays.toString(values), inputs);\r
- }\r
-\r
- public static WireArrayEnd[] extractInputs(WireArray[] w)\r
- {\r
- WireArrayEnd[] inputs = new WireArrayEnd[w.length];\r
- for (int i = 0; i < w.length; i++)\r
- inputs[i] = w[i].createInput();\r
- return inputs;\r
- }\r
-}
\ No newline at end of file
\r
public interface WireArrayObserver\r
{\r
- public void update(WireArray initiator, Bit[] oldValues);\r
+ public void update(Wire initiator, Bit[] oldValues);\r
}\r