22# Licensed under the Apache License, Version 2.0, see LICENSE for details.
33# SPDX-License-Identifier: Apache-2.0
44
5- from systemrdl . rdltypes import OnReadType , OnWriteType , AccessType
6- from systemrdl import node
5+ """Functions with opentitan specific logic."""
6+
77import re
88
9+ from systemrdl import node
10+ from systemrdl .rdltypes import AccessType , OnReadType , OnWriteType
11+
912
1013def register_permit_mask (reg : dict ) -> int :
11- """
12- One bit presents one byte in the register, so in total 4 bits are used.
13- """
14+ """One bit presents one byte in the register, so in total 4 bits are used."""
1415 w = reg ["msb" ] + 1
15- if w > 24 :
16+ if w > 24 : # noqa: PLR2004
1617 return 0b1111
17- if w > 16 :
18+ if w > 16 : # noqa: PLR2004
1819 return 0b0111
19- if w > 8 :
20+ if w > 8 : # noqa: PLR2004
2021 return 0b0011
2122 return 0b0001
2223
2324
24- def needs_read_en (reg : dict () ) -> bool :
25- """Return true if at least one field needs a read-enable
25+ def needs_read_en (reg : dict ) -> bool :
26+ """Return true if at least one field needs a read-enable.
2627
2728 This is true if any of the following are true:
2829
@@ -40,43 +41,42 @@ def needs_read_en(reg: dict()) -> bool:
4041 side might need the re signal)
4142 """
4243 return reg ["shadowed" ] or any (
43- [
44- (field ["clear_onread" ] or (reg ["external" ] and field ["sw_readable" ]))
45- for field in reg ["fields" ]
46- ]
44+ (field ["clear_onread" ] or (reg ["external" ] and field ["sw_readable" ]))
45+ for field in reg ["fields" ]
4746 )
4847
4948
50- def needs_write_en (reg : dict () ) -> bool :
51- """Should the register for this field have a write-enable signal?
49+ def needs_write_en (reg : dict ) -> bool :
50+ """Return register for this field should have a write-enable signal.
5251
5352 This is almost the same as allows_write(), but doesn't return true for
5453 RC registers, which should use a read-enable signal (connected to their
5554 prim_subreg's we port).
5655 """
57- return any ([(not field ["clear_onread" ] and field ["sw_writable" ]) for field in reg ["fields" ]])
56+ return any ((not field ["clear_onread" ] and field ["sw_writable" ]) for field in reg ["fields" ])
57+
5858
59+ def needs_qe (reg : dict ) -> bool :
60+ """Return true if the register or at least one field needs a q-enable."""
61+ return any (field ["swmod" ] for field in reg ["fields" ])
5962
60- def needs_qe (reg : dict ()) -> bool :
61- """Return true if the register or at least one field needs a q-enable"""
62- return any ([field ["swmod" ] for field in reg ["fields" ]])
6363
64+ def needs_int_qe (reg : dict ) -> bool :
65+ """Return true if the register or at least one field needs an internal q-enable.
6466
65- def needs_int_qe (reg : dict ()) -> bool :
66- """Return true if the register or at least one field needs an
67- internal q-enable. An internal q-enable means the net
68- may be consumed by other reg logic but will not be exposed
69- in the package file."""
67+ An internal q-enable means the net may be consumed by other reg logic but will
68+ not be exposed in the package file.
69+ """
7070 return (bool (reg ["async_clk" ]) and reg ["hw_writable" ]) or needs_qe (reg )
7171
7272
7373def get_bit_width (offset : int ) -> int :
74- """Calculate the number of bits to address every byte of the block"""
74+ """Calculate the number of bits to address every byte of the block. """
7575 return (offset - 1 ).bit_length ()
7676
7777
7878def get_sw_access_enum (field : node .FieldNode ) -> str :
79- """Map the rdl access permissions to reggen SwAccess enum"""
79+ """Map the rdl access permissions to reggen SwAccess enum. """
8080 sw = field .get_property ("sw" )
8181 onwrite = field .get_property ("onwrite" )
8282 onread = field .get_property ("onread" )
@@ -98,15 +98,17 @@ def get_sw_access_enum(field: node.FieldNode) -> str:
9898 return "NONE"
9999
100100
101- def fields_no_write_en (reg : dict ()) -> int :
101+ def fields_no_write_en (reg : dict ) -> int :
102+ """Count how many fields has write enable."""
102103 res = 0
103104 for idx , field in enumerate (reg ["fields" ]):
104105 res |= (not needs_we (field )) << idx
105106 return res
106107
107108
108109def needs_we (field : dict ) -> bool :
109- """Should the register for this field have a write-enable signal?
110+ """True if the register for this field should have a write-enable signal.
111+
110112 This is almost the same as allows_write(), but doesn't return true for
111113 RC registers, which should use a read-enable signal (connected to their
112114 prim_subreg's we port).
@@ -115,8 +117,9 @@ def needs_we(field: dict) -> bool:
115117
116118
117119def is_homogeneous (reg : dict ) -> bool :
118- """Return true if all fields of a register are equal. The offset are excluded from
119- the comparison.
120+ """Return true if all fields of a register are equal.
121+
122+ The offset are excluded from the comparison.
120123 """
121124 exclude = ["name" , "msb" , "lsb" , "bitmask" , "type" ]
122125 unamed_fields = [
0 commit comments