Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
simuDAtor
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Computer Engineering
simuDAtor
Commits
39d5fb08
Commit
39d5fb08
authored
1 year ago
by
Johannes Kung
Browse files
Options
Downloads
Patches
Plain Diff
Refactored memory
parent
021acba0
No related branches found
No related tags found
1 merge request
!27
Docstring refactor of core
Pipeline
#131795
failed
1 year ago
Stage: test
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/simudator/core/modules/memory.py
+60
-55
60 additions, 55 deletions
src/simudator/core/modules/memory.py
src/simudator/processor/mia/modules/mia_memory.py
+11
-9
11 additions, 9 deletions
src/simudator/processor/mia/modules/mia_memory.py
with
71 additions
and
64 deletions
src/simudator/core/modules/memory.py
+
60
−
55
View file @
39d5fb08
from
__future__
import
annotations
from
typing
import
Any
from
simudator.core.module
import
Module
from
simudator.core.signal
import
Signal
# Used to pad the values of the memory when saving to file
VALUE_PADDING
=
2
ADDRESS_PADDING
=
2
class
Memory
(
Module
):
"""
A general size memory module.
Parameters
----------
input : Signal
Signal of which the value may be written to the current address.
output : Signal
Signal to which the content of the current address is written.
control : Signal
Signal used to enable or disable write to the current address. Assumed
to output ``bool``.
address : Signal
Signal used to address the memory. Assumed to output ``int``.
size : int
Size of the address space of the memory.
name : str
Name of the module.
"""
def
__init__
(
self
,
input
_signal
:
Signal
,
output
_signal
:
Signal
,
control
_signal
:
Signal
,
address
_signal
:
Signal
,
input
:
Signal
,
output
:
Signal
,
control
:
Signal
,
address
:
Signal
,
size
:
int
,
value_padding
:
int
=
2
,
address_padding
:
int
=
2
,
name
=
"
Memory
"
,
)
->
None
:
# signals
signals
=
{
"
in_input
"
:
input
_signal
,
"
in_control
"
:
control
_signal
,
"
in_address
"
:
address
_signal
,
"
out_content
"
:
output
_signal
"
in_input
"
:
input
,
"
in_control
"
:
control
,
"
in_address
"
:
address
,
"
out_content
"
:
output
,
}
# Init super class
super
().
__init__
(
signals
,
name
)
# Internal state
self
.
memory
=
[
0
for
i
in
range
(
size
)]
self
.
current_address
=
0
self
.
is_write
=
False
# Values used to format the strings when saving state to file
self
.
address_padding
=
address_padding
self
.
value_padding
=
value_padding
self
.
_memory
=
[
0
for
_
in
range
(
size
)]
self
.
_current_address
=
0
self
.
_is_write
=
False
def
update_register
(
self
):
if
self
.
is_write
:
self
.
memory
[
self
.
current_address
]
=
self
.
signals
[
"
in_input
"
].
get_value
()
if
self
.
_is_write
:
value
=
self
.
signals
[
"
in_input
"
].
get_value
()
self
.
_memory
[
self
.
_current_address
]
=
value
def
output_register
(
self
)
->
None
:
pass
...
...
@@ -54,59 +71,50 @@ class Memory(Module):
adr_sig
=
self
.
signals
[
"
in_address
"
]
ctrl_sig
=
self
.
signals
[
"
in_control
"
]
out_sig
=
self
.
signals
[
"
out_content
"
]
if
(
adr_sig
.
get_value
()
is
not
None
and
ctrl_sig
.
get_value
()
is
not
None
):
self
.
is_write
=
ctrl_sig
.
get_value
()
self
.
current_address
=
adr_sig
.
get_value
()
out_sig
.
update_value
(
self
.
memory
[
self
.
current_address
])
if
adr_sig
.
get_value
()
is
not
None
and
ctrl_sig
.
get_value
()
is
not
None
:
self
.
_is_write
=
ctrl_sig
.
get_value
()
self
.
_current_address
=
adr_sig
.
get_value
()
out_sig
.
update_value
(
self
.
_memory
[
self
.
_current_address
])
def
print_module
(
self
)
->
None
:
print
(
""
,
self
.
name
,
"
\n
-----
"
)
for
address
,
value
in
enumerate
(
self
.
memory
):
for
address
,
value
in
enumerate
(
self
.
_
memory
):
if
value
:
print
(
""
,
str
(
hex
(
address
)),
""
,
str
(
hex
(
value
)),
"
\n
"
)
def
get_state
(
self
)
->
dict
:
def
get_state
(
self
)
->
dict
[
str
,
Any
]
:
state
=
super
().
get_state
()
state
[
"
is_write
"
]
=
self
.
is_write
state
[
"
current_address
"
]
=
self
.
current_address
state
[
"
memory
"
]
=
self
.
memory
[:]
state
[
"
is_write
"
]
=
self
.
_
is_write
state
[
"
current_address
"
]
=
self
.
_
current_address
state
[
"
memory
"
]
=
self
.
_
memory
[:]
return
state
def
get_gui_state
(
self
)
->
dict
:
def
get_gui_state
(
self
)
->
dict
[
str
,
Any
]
:
state
=
super
().
get_gui_state
()
state
[
"
current_address
"
]
=
self
.
current_address
state
[
"
memory
"
]
=
self
.
memory
[:]
state
[
"
current_address
"
]
=
self
.
_
current_address
state
[
"
memory
"
]
=
self
.
_
memory
[:]
return
state
def
set_state
(
self
,
state
:
dict
)
->
None
:
def
set_state
(
self
,
state
:
dict
[
str
,
Any
]
)
->
None
:
super
().
set_state
(
state
)
if
"
is_write
"
in
state
:
self
.
is_write
=
state
[
"
is_write
"
]
self
.
_
is_write
=
state
[
"
is_write
"
]
if
"
current_address
"
in
state
:
self
.
current_address
=
state
[
"
current_address
"
]
self
.
_
current_address
=
state
[
"
current_address
"
]
if
"
memory
"
in
state
:
self
.
memory
=
state
[
"
memory
"
]
self
.
_
memory
=
state
[
"
memory
"
]
def
reset
(
self
)
->
None
:
"""
Reset the memory to 0 for each address.
"""
for
i
in
range
(
len
(
self
.
memory
)):
self
.
memory
[
i
]
=
0
for
i
in
range
(
len
(
self
.
_
memory
)):
self
.
_
memory
[
i
]
=
0
def
get_longest_line_len
(
self
,
ignore_keys
=
[])
->
int
:
"""
Helper function for pretty_print that returns the length of
the longest value in the memory to print for a module.
"""
longest_memory_line
=
0
for
value
in
self
.
memory
:
for
value
in
self
.
_
memory
:
value_len
=
len
(
str
(
value
))
if
value_len
>
longest_memory_line
:
longest_memory_line
=
value_len
...
...
@@ -118,19 +126,16 @@ class Memory(Module):
Helper function for pretty_print that returns the length of
the largest address in the memory to print for a module.
"""
return
len
(
str
(
len
(
self
.
memory
)))
return
len
(
str
(
len
(
self
.
_
memory
)))
def
save_state_to_file
(
self
,
file_path
:
str
)
->
None
:
"""
Tries to save the modules state to a given file.
"""
def
save_state_to_file
(
self
,
file_path
:
str
)
->
bool
:
file
=
open
(
file_path
,
"
a
"
)
file
.
write
(
self
.
name
+
"
:
\n
"
)
for
index
,
value
in
enumerate
(
self
.
memory
):
for
index
,
value
in
enumerate
(
self
.
_
memory
):
# Write the address in hex and add ': ' to the end
file
.
write
(
hex
(
index
)[
2
:].
rjust
(
self
.
address_padding
,
"
0
"
)
+
"
:
"
)
file
.
write
(
hex
(
index
)[
2
:].
rjust
(
ADDRESS_PADDING
,
"
0
"
)
+
"
:
"
)
# Write the value in hex
file
.
write
(
hex
(
value
)[
2
:].
rjust
(
self
.
value_padding
,
"
0
"
))
file
.
write
(
hex
(
value
)[
2
:].
rjust
(
VALUE_PADDING
,
"
0
"
))
file
.
write
(
"
\n
"
)
file
.
write
(
"
\n
"
)
...
...
This diff is collapsed.
Click to expand it.
src/simudator/processor/mia/modules/mia_memory.py
+
11
−
9
View file @
39d5fb08
...
...
@@ -32,8 +32,6 @@ class MiaMemory(MiaBusConnector, Memory):
address_signal
,
size
,
name
=
name
,
value_padding
=
value_padding
,
address_padding
=
address_padding
,
)
self
.
bus_control_s
.
add_destination
(
self
)
...
...
@@ -45,14 +43,16 @@ class MiaMemory(MiaBusConnector, Memory):
Updates the value of the current address.
"""
if
self
.
read_from_bus
():
self
.
memory
[
self
.
current_address
]
=
self
.
signals
[
"
in_input
"
].
get_value
()
self
.
_
memory
[
self
.
_
current_address
]
=
self
.
signals
[
"
in_input
"
].
get_value
()
def
output_register
(
self
)
->
None
:
"""
Outputs the value of the current address.
"""
if
self
.
write_to_bus
():
self
.
signals
[
"
out_content
"
].
update_value
(
self
.
memory
[
self
.
current_address
])
self
.
signals
[
"
out_content
"
].
update_value
(
self
.
_memory
[
self
.
_current_address
]
)
else
:
self
.
signals
[
"
out_content
"
].
update_value
(
None
)
...
...
@@ -63,15 +63,17 @@ class MiaMemory(MiaBusConnector, Memory):
"""
address_value
=
self
.
signals
[
"
in_address
"
].
get_value
()
if
address_value
is
not
None
:
self
.
current_address
=
address_value
self
.
_
current_address
=
address_value
if
self
.
write_to_bus
():
self
.
signals
[
"
out_content
"
].
update_value
(
self
.
memory
[
self
.
current_address
])
self
.
signals
[
"
out_content
"
].
update_value
(
self
.
_memory
[
self
.
_current_address
]
)
else
:
self
.
signals
[
"
out_content
"
].
update_value
(
None
)
def
reset
(
self
)
->
None
:
size
=
len
(
self
.
memory
)
self
.
memory
=
[
0
for
i
in
range
(
size
)]
size
=
len
(
self
.
_
memory
)
self
.
_
memory
=
[
0
for
i
in
range
(
size
)]
def
load_from_str
(
self
,
state_string
)
->
None
:
"""
...
...
@@ -89,7 +91,7 @@ class MiaMemory(MiaBusConnector, Memory):
# Last character of the adress is a semicolon
adress
=
int
(
line_data
[
0
][:
-
1
],
16
)
self
.
memory
[
adress
]
=
value
self
.
_
memory
[
adress
]
=
value
# There is an asm instruction label
if
len
(
line_data
)
==
3
:
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment