@@ -131,14 +131,12 @@ def __init__(self, resource, path, start, end, width):
131
131
all (name and isinstance (name , str ) for name in flattened_path )):
132
132
raise TypeError (f"Path must be a non-empty tuple of non-empty strings, not { path !r} " )
133
133
if not isinstance (start , int ) or start < 0 :
134
- raise TypeError ("Start address must be a non-negative integer, not {!r}"
135
- .format (start ))
134
+ raise TypeError (f"Start address must be a non-negative integer, not { start !r} " )
136
135
if not isinstance (end , int ) or end <= start :
137
- raise TypeError ("End address must be an integer greater than the start address, "
138
- "not {!r}" . format ( end ) )
136
+ raise TypeError (f "End address must be an integer greater than the start address, "
137
+ f "not { end !r} " )
139
138
if not isinstance (width , int ) or width < 0 :
140
- raise TypeError ("Width must be a non-negative integer, not {!r}"
141
- .format (width ))
139
+ raise TypeError (f"Width must be a non-negative integer, not { width !r} " )
142
140
143
141
self ._resource = resource
144
142
self ._path = tuple (path )
@@ -199,16 +197,13 @@ class MemoryMap:
199
197
"""
200
198
def __init__ (self , * , addr_width , data_width , alignment = 0 , name = None ):
201
199
if not isinstance (addr_width , int ) or addr_width <= 0 :
202
- raise ValueError ("Address width must be a positive integer, not {!r}"
203
- .format (addr_width ))
200
+ raise ValueError (f"Address width must be a positive integer, not { addr_width !r} " )
204
201
if not isinstance (data_width , int ) or data_width <= 0 :
205
- raise ValueError ("Data width must be a positive integer, not {!r}"
206
- .format (data_width ))
202
+ raise ValueError (f"Data width must be a positive integer, not { data_width !r} " )
207
203
if not isinstance (alignment , int ) or alignment < 0 :
208
- raise ValueError ("Alignment must be a non-negative integer, not {!r}"
209
- .format (alignment ))
204
+ raise ValueError (f"Alignment must be a non-negative integer, not { alignment !r} " )
210
205
if name is not None and not (isinstance (name , str ) and name ):
211
- raise ValueError ("Name must be a non-empty string, not {!r}" . format ( name ) )
206
+ raise ValueError (f "Name must be a non-empty string, not { name !r} " )
212
207
213
208
self ._addr_width = addr_width
214
209
self ._data_width = data_width
@@ -267,32 +262,28 @@ def align_to(self, alignment):
267
262
Implicit next address.
268
263
"""
269
264
if not isinstance (alignment , int ) or alignment < 0 :
270
- raise ValueError ("Alignment must be a non-negative integer, not {!r}"
271
- .format (alignment ))
265
+ raise ValueError (f"Alignment must be a non-negative integer, not { alignment !r} " )
272
266
self ._next_addr = self ._align_up (self ._next_addr , max (alignment , self .alignment ))
273
267
return self ._next_addr
274
268
275
269
def _compute_addr_range (self , addr , size , step = 1 , * , alignment ):
276
270
if addr is not None :
277
271
if not isinstance (addr , int ) or addr < 0 :
278
- raise ValueError ("Address must be a non-negative integer, not {!r}"
279
- .format (addr ))
272
+ raise ValueError (f"Address must be a non-negative integer, not { addr !r} " )
280
273
if addr % (1 << self .alignment ) != 0 :
281
- raise ValueError ("Explicitly specified address {:#x} must be a multiple of "
282
- "{:#x} bytes"
283
- .format (addr , 1 << alignment ))
274
+ raise ValueError (f"Explicitly specified address { addr :#x} must be a multiple of "
275
+ f"{ 1 << alignment :#x} bytes" )
284
276
else :
285
277
addr = self ._align_up (self ._next_addr , alignment )
286
278
287
279
if not isinstance (size , int ) or size < 0 :
288
- raise ValueError ("Size must be a non-negative integer, not {!r}"
289
- .format (size ))
280
+ raise ValueError (f"Size must be a non-negative integer, not { size !r} " )
290
281
size = self ._align_up (max (size , 1 ), alignment )
291
282
292
283
if addr > (1 << self .addr_width ) or addr + size > (1 << self .addr_width ):
293
- raise ValueError ("Address range {:#x}..{:#x} out of bounds for memory map spanning "
294
- " range {:#x}..{:#x} ({} address bits) "
295
- . format ( addr , addr + size , 0 , 1 << self .addr_width , self . addr_width ) )
284
+ raise ValueError (f "Address range { addr :#x} ..{ addr + size :#x} out of bounds for "
285
+ f"memory map spanning range { 0 :#x} ..{ 1 << self . addr_width :#x} "
286
+ f"( { self .addr_width } address bits)" )
296
287
297
288
addr_range = range (addr , addr + size , step )
298
289
overlaps = self ._ranges .overlaps (addr_range )
@@ -301,14 +292,14 @@ def _compute_addr_range(self, addr, size, step=1, *, alignment):
301
292
for overlap in overlaps :
302
293
if id (overlap ) in self ._resources :
303
294
_ , _ , resource_range = self ._resources [id (overlap )]
304
- overlap_descrs .append ("resource {!r} at {:#x}..{:#x} "
305
- . format ( overlap , resource_range . start , resource_range .stop ) )
295
+ overlap_descrs .append (f "resource { overlap !r} at { resource_range . start :#x} .."
296
+ f" { resource_range .stop :#x } " )
306
297
if id (overlap ) in self ._windows :
307
298
_ , window_range = self ._windows [id (overlap )]
308
- overlap_descrs .append ("window {!r} at {:#x}..{:#x} "
309
- . format ( overlap , window_range . start , window_range .stop ) )
310
- raise ValueError ("Address range {:#x}..{:#x} overlaps with {}"
311
- . format ( addr , addr + size , ", " .join (overlap_descrs ) ))
299
+ overlap_descrs .append (f "window { overlap !r} at { window_range . start :#x} .."
300
+ f" { window_range .stop :#x } " )
301
+ raise ValueError (f "Address range { addr :#x} ..{ addr + size :#x} overlaps with " +
302
+ ", " .join (overlap_descrs ))
312
303
313
304
return addr_range
314
305
@@ -355,16 +346,15 @@ def add_resource(self, resource, *, name, size, addr=None, alignment=None):
355
346
this memory map.
356
347
"""
357
348
if self ._frozen :
358
- raise ValueError ("Memory map has been frozen. Cannot add resource {!r}"
359
- .format (resource ))
349
+ raise ValueError (f"Memory map has been frozen. Cannot add resource { resource !r} " )
360
350
361
351
if not isinstance (resource , wiring .Component ):
362
352
raise TypeError (f"Resource must be a wiring.Component, not { resource !r} " )
363
353
364
354
if id (resource ) in self ._resources :
365
355
_ , _ , addr_range = self ._resources [id (resource )]
366
- raise ValueError ("Resource {!r} is already added at address range {:#x}..{:#x} "
367
- . format ( resource , addr_range .start , addr_range .stop ) )
356
+ raise ValueError (f "Resource { resource !r} is already added at address range "
357
+ f" { addr_range .start :#x } .. { addr_range .stop :#x } " )
368
358
369
359
if not (name and isinstance (name , tuple ) and
370
360
all (part and isinstance (part , str ) for part in name )):
@@ -380,8 +370,7 @@ def add_resource(self, resource, *, name, size, addr=None, alignment=None):
380
370
381
371
if alignment is not None :
382
372
if not isinstance (alignment , int ) or alignment < 0 :
383
- raise ValueError ("Alignment must be a non-negative integer, not {!r}"
384
- .format (alignment ))
373
+ raise ValueError (f"Alignment must be a non-negative integer, not { alignment !r} " )
385
374
alignment = max (alignment , self .alignment )
386
375
else :
387
376
alignment = self .alignment
@@ -470,33 +459,28 @@ def add_window(self, window, *, addr=None, sparse=None):
470
459
conflicts with the name of others present in this memory map.
471
460
"""
472
461
if not isinstance (window , MemoryMap ):
473
- raise TypeError ("Window must be a MemoryMap, not {!r}"
474
- .format (window ))
462
+ raise TypeError (f"Window must be a MemoryMap, not { window !r} " )
475
463
476
464
if self ._frozen :
477
- raise ValueError ("Memory map has been frozen. Cannot add window {!r}"
478
- .format (window ))
465
+ raise ValueError (f"Memory map has been frozen. Cannot add window { window !r} " )
479
466
480
467
if id (window ) in self ._windows :
481
468
_ , addr_range = self ._windows [id (window )]
482
- raise ValueError ("Window {!r} is already added at address range {:#x}..{:#x} "
483
- . format ( window , addr_range .start , addr_range .stop ) )
469
+ raise ValueError (f "Window { window !r} is already added at address range "
470
+ f" { addr_range .start :#x } .. { addr_range .stop :#x } " )
484
471
485
472
if window .data_width > self .data_width :
486
- raise ValueError ("Window has data width {}, and cannot be added to a memory map "
487
- "with data width {}"
488
- .format (window .data_width , self .data_width ))
473
+ raise ValueError (f"Window has data width { window .data_width } , and cannot be added to a "
474
+ f"memory map with data width { self .data_width } " )
489
475
if window .data_width != self .data_width :
490
476
if sparse is None :
491
- raise ValueError ("Address translation mode must be explicitly specified "
492
- "when adding a window with data width {} to a memory map "
493
- "with data width {}"
494
- .format (window .data_width , self .data_width ))
477
+ raise ValueError (f"Address translation mode must be explicitly specified when "
478
+ f"adding a window with data width { window .data_width } to a "
479
+ f"memory map with data width { self .data_width } " )
495
480
if not sparse and self .data_width % window .data_width != 0 :
496
- raise ValueError ("Dense addressing cannot be used because the memory map "
497
- "data width {} is not an integer multiple of window "
498
- "data width {}"
499
- .format (self .data_width , window .data_width ))
481
+ raise ValueError (f"Dense addressing cannot be used because the memory map "
482
+ f"data width { self .data_width } is not an integer multiple of "
483
+ f"window data width { window .data_width } " )
500
484
501
485
queries = window ._namespace .names () if window .name is None else ((window .name ,),)
502
486
reasons = []
@@ -564,10 +548,10 @@ def window_patterns(self):
564
548
for window , (window_start , window_stop , window_ratio ) in self .windows ():
565
549
const_bits = self .addr_width - window .addr_width
566
550
if const_bits > 0 :
567
- const_pat = "{:0{}b}" . format ( window_start >> window .addr_width , const_bits )
551
+ const_pat = f" { window_start >> window .addr_width :0{ const_bits }b } "
568
552
else :
569
553
const_pat = ""
570
- pattern = "{}{}" . format ( const_pat , "-" * window .addr_width )
554
+ pattern = const_pat + "-" * window .addr_width
571
555
yield window , (pattern , window_ratio )
572
556
573
557
@staticmethod
0 commit comments