File size: 15,480 Bytes
f5dbfe6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
from amaranth import *
from amaranth.sim import Simulator
from amaranth.lib.wiring import Component, In, Out

from registers2 import* # register mapping


class Example_Module(Component):
    # Example module that does nothing

    def __init__(self, number_of_instances=1):
        """
        Init is called upon instantiation of the module.
        Any custom parameters can be passed here.
        """

        # Validate the number of instances
        assert number_of_instances > 0
        assert number_of_instances <= 4
        
        # just an example, not actually used
        self.number_of_instances = number_of_instances

        super().__init__({  # create any ports into/out of the module here
            
            # memory access ports, required for all modules
            "bram_address": In(16),
            "bram_write_data": In(32),
            "bram_read_data": Out(32),
            "bram_write_enable": In(1),
            # TODO: make this an amaranth interface

            # math/processing modules may have only the memory access ports

            # modules with external IO will have additional ports
            "tx": Out(1),
            "rx": In(1),

            
            # debug is useful for connecting to a logic analyzer for final testing, not required
            "debug": Out(8)
        })

        # Any driver settings will be passed to the software driver
        # typically for specifying how the driver is configured (ex: how many encoders were instantiated)
        driver_settings = {
            "number_of_instances": self.number_of_instances,
            "version": "1.0.0",  # add versioning if multiple versions of the module are used
        }

        # Create the register map for this module
        self.rm = RegisterMapGenerator("example_module", compatible_drivers=["example_module"], driver_settings=driver_settings, desc="Example Module Description")
        # compatible_drivers is a list of software driver names that can use this module,
        #   if none are available, the module will not automatically be acessible from the software


        # Add registers to the register map

        # All physical registers are 32 bits wide, but actual data may be less than that

        # individual registers
        self.rm.add(Register("example_register_0", rw="r", type="signed", width=32, desc="Example register description 0"))
        self.rm.add(Register("example_register_1", rw="w", type="unsigned", width=16, desc="Example register description 1"))
        self.rm.add(Register("example_register_2", rw="r", type="unsigned", width=5, desc="Example register description 2"))

        # array of registers
        self.rm.add(Register("example_register_array", rw="w", type="signed", width=32, desc="Example register array description", bank_size=32))
        # 32 registers in the array, registers may be of any type or size, but will still take 32 consecutive addresses

        # packeted registers
        # it is a waste of space to have a full 32 bit memory space if you only have boolean or small values
        # so you can use a packed register to save space
        # it combines multiple sub-registers into a single register
        self.rm.add(Register("example_packed_register", rw="r", desc="Encoder status", sub_registers=[
            Register("small_flag_0", type="bool", desc="single bit"),
            Register("small_flag_1", type="bool", desc="single bit"),
            Register("small_flag_2", type="bool", desc="single bit"),
            Register("small_value_0", type="unsigned", width=8, desc="small value"),
            Register("small_value_1", type="unsigned", width=8, desc="another small value"),
        ]))
        # note that all sub-registers will inherit the rw permissions of the parent register
        # sub-registers may be placed at specific starting bits using the 'start_address'

        # any registers may be placed at a specific address using the 'start_address' parameter, placement is automatic otherwise


        # groups
        # in many cases it is useful to group registers together and have multiple sets of them
        example_group = Group("example_group", count=4, start_address=0x0, desc="Group of registers example")

        # you may add as many registers as you like to a group, including arrays and packed registers
        # TODO: do groups of groups work?
        example_group.add(Register("group_register_0", rw="r", type="unsigned", width=16, desc="Group register 0"))
        example_group.add(Register("group_register_1", rw="r", type="unsigned", width=16, desc="Group register 1"))
        example_group.add(Register("group_register_2", rw="w", type="unsigned", width=16, desc="Group register 2"))
    
        # you can also specify the start address of the group, if not placement is automatic
        # if the 'alignement' parameter is not set, it will be set to the smallest size that fits all registers and is a power of 2

        # don't forget to add the group to the register map after filling it
        self.rm.add(example_group)

        # generate the register map
        self.rm.generate()


    def elaborate(self, platform):
        """
        Elaborate is called to create the hardware description of the module.
        This is where you define the logic of the module.
        """

        m = Module()

        m.d.sync_100 += self.debug.eq(0)

        # no logic is shown in this example
        # see the amaranth documentation for more information on how to create these.

        # there are 4 syncronized clocks to choose from:
        # m.d.sync_200  # 200 MHz clock
        # m.d.sync_100  # 100 MHz clock (memory interface uses this clock)
        # m.d.sync_50   # 50 MHz clock
        # m.d.sync_25   # 25 MHz clock

        # most modules will use only the 100 MHz clock as it is still pretty easy to meet timing requirements with it
        

        # accessing the generated register map

        """
        Registers all include the following properties:
            - address_offset: the address offset of the register in the memory map,
                relative to the start of the containing group if applicable
            - width: the width of the register in bits
            - starting_bit: the starting bit of the register (0 for full registers, will vary for sub-registers)
            - bank_size: the size of the register bank, (1 for individual registers, larger for arrays)
            - description: a description of the group
        """
        # get the address of a register
        example_register_0_address = self.rm.example_register_0.address_offset
        
        # get the width of a register
        example_register_0_width = self.rm.example_register_0.width


        """
        Groups include the following properties:
            - offset: the starting address of the group in the memory map,
                relative to the start of the containing group if applicable
            - count: the number of instances of the group
            - alignment: the alignment of the group address space (default is the smallest power of 2 that fits all registers)
                power of 2 alignment makes addressing significantly easier and more efficient than using the smallest size
            - description: a description of the group
        """
        # get the address of a group
        example_group_address = self.rm.example_group.offset
        # get the count of a group
        example_group_count = self.rm.example_group.count
        # get the alignment of a group
        example_group_alignment = self.rm.example_group.alignment

        # resulting actual absolute address of the group set would be:
        group_index = 0  # index of the group instance, 0 for the first instance
        absolute_address = example_group_address + (example_group_alignment * group_index)

        # then that address can be used when referencing the registers in the group
        # for example, to access the group_register_0 register absolute address:
        group_0_register_0_address = absolute_address + self.rm.example_group.group_register_0.address_offset



        return m
    


dut = Example_Module(number_of_instances=2)

async def bench(ctx):
    # testbench for the Example_Module

    # run your own tests here
    # check the amaranth documentation for more information on how to create a testbench

    pass



if __name__ == "__main__":

    #dut.rm.exportJSON("example_module.json")  # just for viewing, not required to be done

    sim = Simulator(dut)

    #sim.add_clock(1/200e6, domain="sync_200")  # no need to add if we aren't using it
    sim.add_clock(1/100e6, domain="sync_100")
    #sim.add_clock(1/50e6, domain="sync_50")
    #sim.add_clock(1/25e6, domain="sync_25")
    

    # add the testbench
    sim.add_testbench(bench)

    # run the simulation
    with sim.write_vcd("example_module.vcd"):
        sim.run()

    # you can now open the generated VCD file in a waveform viewer


# JSON export register map for reference
"""
{
    "name": "example_module",
    "compatible_drivers": [
        "example_module"
    ],
    "driver_settings": {
        "number_of_instances": 2,
        "version": "1.0.0"
    },
    "base_group": {
        "name": "base_group",
        "address_offset": 0,
        "description": "Base group for all registers",
        "alignment": 65536,
        "count": 1,
        "groups": {
            "example_group": {
                "name": "example_group",
                "address_offset": 0,
                "description": "Group of registers example",
                "alignment": 4,
                "count": 4,
                "groups": {},
                "registers": {
                    "group_register_0": {
                        "name": "group_register_0",
                        "address_offset": 0,
                        "type": "unsigned",
                        "bank_size": 1,
                        "description": "Group register 0",
                        "width": 16,
                        "starting_bit": 0,
                        "sub_registers": {},
                        "rw": "r"
                    },
                    "group_register_1": {
                        "name": "group_register_1",
                        "address_offset": 1,
                        "type": "unsigned",
                        "bank_size": 1,
                        "description": "Group register 1",
                        "width": 16,
                        "starting_bit": 0,
                        "sub_registers": {},
                        "rw": "r"
                    },
                    "group_register_2": {
                        "name": "group_register_2",
                        "address_offset": 2,
                        "type": "unsigned",
                        "bank_size": 1,
                        "description": "Group register 2",
                        "width": 16,
                        "starting_bit": 0,
                        "sub_registers": {},
                        "rw": "w"
                    }
                }
            }
        },
        "registers": {
            "example_register_0": {
                "name": "example_register_0",
                "address_offset": 16,
                "type": "signed",
                "bank_size": 1,
                "description": "Example register description 0",
                "width": 32,
                "starting_bit": 0,
                "sub_registers": {},
                "rw": "r"
            },
            "example_register_1": {
                "name": "example_register_1",
                "address_offset": 17,
                "type": "unsigned",
                "bank_size": 1,
                "description": "Example register description 1",
                "width": 16,
                "starting_bit": 0,
                "sub_registers": {},
                "rw": "w"
            },
            "example_register_2": {
                "name": "example_register_2",
                "address_offset": 18,
                "type": "unsigned",
                "bank_size": 1,
                "description": "Example register description 2",
                "width": 5,
                "starting_bit": 0,
                "sub_registers": {},
                "rw": "r"
            },
            "example_register_array": {
                "name": "example_register_array",
                "address_offset": 19,
                "type": "signed",
                "bank_size": 32,
                "description": "Example register array description",
                "width": 32,
                "starting_bit": 0,
                "sub_registers": {},
                "rw": "w"
            },
            "example_packed_register": {
                "name": "example_packed_register",
                "address_offset": 51,
                "type": "unsigned",
                "bank_size": 1,
                "description": "Encoder status",
                "width": 32,
                "starting_bit": 0,
                "sub_registers": {
                    "small_flag_0": {
                        "name": "small_flag_0",
                        "address_offset": 51,
                        "type": "bool",
                        "bank_size": 1,
                        "description": "single bit",
                        "width": 1,
                        "starting_bit": 0,
                        "sub_registers": {},
                        "rw": "r"
                    },
                    "small_flag_1": {
                        "name": "small_flag_1",
                        "address_offset": 51,
                        "type": "bool",
                        "bank_size": 1,
                        "description": "single bit",
                        "width": 1,
                        "starting_bit": 1,
                        "sub_registers": {},
                        "rw": "r"
                    },
                    "small_flag_2": {
                        "name": "small_flag_2",
                        "address_offset": 51,
                        "type": "bool",
                        "bank_size": 1,
                        "description": "single bit",
                        "width": 1,
                        "starting_bit": 2,
                        "sub_registers": {},
                        "rw": "r"
                    },
                    "small_value_0": {
                        "name": "small_value_0",
                        "address_offset": 51,
                        "type": "unsigned",
                        "bank_size": 1,
                        "description": "small value",
                        "width": 8,
                        "starting_bit": 3,
                        "sub_registers": {},
                        "rw": "r"
                    },
                    "small_value_1": {
                        "name": "small_value_1",
                        "address_offset": 51,
                        "type": "unsigned",
                        "bank_size": 1,
                        "description": "another small value",
                        "width": 8,
                        "starting_bit": 11,
                        "sub_registers": {},
                        "rw": "r"
                    }
                },
                "rw": "r"
            }
        }
    }
}
"""