Class: Csr
- Inherits:
-
ArchDefObject
- Object
- ArchDefObject
- Csr
- Defined in:
- lib/arch_obj_models/csr.rb
Overview
CSR definition
Instance Method Summary collapse
- #==(other) ⇒ Object
- #address ⇒ Integer?
- #base ⇒ Integer?
-
#bitfield_type(arch_def, effective_xlen = nil) ⇒ Idl::BitfieldType
A bitfield type that can represent all fields of the CSR.
-
#defined_in_all_bases? ⇒ Boolean
True if this CSR is defined regardless of the effective XLEN.
-
#defined_in_base32? ⇒ Boolean
True if this CSR is defined when XLEN is 32.
-
#defined_in_base64? ⇒ Boolean
True if this CSR is defined when XLEN is 64.
-
#description_html ⇒ String
parse description field with asciidoctor, and return the HTML result.
-
#dynamic_length?(arch_def) ⇒ Boolean
Whether or not the length of the CSR depends on a runtime value (e.g., mstatus.SXL).
-
#exists_in_cfg?(arch_def) ⇒ Boolean
Whether or not the CSR is possibly implemented given the supplies config options.
-
#field(field_name) ⇒ Object
returns [CsrField,nil] field named ‘field_name’ if it exists, and nil otherwise.
-
#field?(field_name) ⇒ Boolean
True if a field named ‘field_name’ is defined in the csr, and false otherwise.
-
#field_hash ⇒ Hash<String,CsrField>
Hash of fields, indexed by field name.
-
#fields ⇒ Array<CsrField>
All known fields of this CSR.
-
#fields_for(effective_xlen) ⇒ Array<CsrField>
equivalent to #fields if
effective_xlen
is nil. -
#format_changes_with_xlen?(arch_def) ⇒ Boolean
Whether or not the format of this CSR changes when the effective XLEN changes in some mode.
-
#has_custom_sw_read? ⇒ Boolean
True if the CSR has a custom sw_read function.
-
#implemented_fields(arch_def) ⇒ Array<CsrField>
All implemented fields for this CSR Excluded any fields that are defined by unimplemented extensions.
-
#implemented_fields_for(arch_def, effective_xlen) ⇒ Array<CsrField>
All implemented fields for this CSR at the given effective XLEN, sorted by location (smallest location first) Excluded any fields that are defined by unimplemented extensions or a base that is not effective_xlen.
- #length(arch_def, effective_xlen = nil) ⇒ Integer?
-
#length_cond32 ⇒ String
IDL condition of when the effective xlen is 32.
-
#length_cond64 ⇒ String
IDL condition of when the effective xlen is 64.
-
#length_pretty(arch_def, effective_xlen = nil) ⇒ String
Pretty-printed length string.
- #long_name ⇒ Object
-
#max_length(arch_def) ⇒ Integer
The largest length of this CSR in any valid mode/xlen for the config.
-
#min_length(arch_def) ⇒ Integer
Smallest length of the CSR in any mode.
-
#modes_with_access ⇒ Object
list of modes that can potentially access the field.
-
#optional_in_cfg?(arch_def) ⇒ Boolean
Whether or not the CSR is optional in the config.
-
#priv_mode ⇒ String
Least-privileged mode that can access this CSR.
- #pruned_sw_read_ast(arch_def) ⇒ Object
-
#reachable_functions(arch_def) ⇒ Array<Idl::FunctionDefAst>
List of functions reachable from this CSR’s sw_read or a field’s sw_wirte function.
-
#reachable_functions_unevaluated(arch_def) ⇒ Array<Idl::FunctionDefAst>
List of functions reachable from this CSR’s sw_read or a field’s sw_wirte function, irrespective of context.
-
#sw_read_ast(symtab) ⇒ FunctionBodyAst
The abstract syntax tree of the sw_read() function.
- #type_checked_sw_read_ast(symtab) ⇒ Object
- #virtual_address ⇒ Integer?
-
#wavedrom_desc(arch_def, effective_xlen, exclude_unimplemented: false, optional_type: 2) ⇒ Hash
A representation of the WaveDrom drawing for the CSR (should be turned into JSON for wavedrom).
Constructor Details
This class inherits a constructor from ArchDefObject
Instance Method Details
#==(other) ⇒ Object
8 9 10 11 12 13 14 |
# File 'lib/arch_obj_models/csr.rb', line 8 def ==(other) if other.is_a?(Csr) name == other.name else raise ArgumentError, "Csr is not comparable to #{other.class.name}" end end |
#address ⇒ Integer?
18 19 20 |
# File 'lib/arch_obj_models/csr.rb', line 18 def address @data["address"] end |
#base ⇒ Integer?
39 |
# File 'lib/arch_obj_models/csr.rb', line 39 def base = @data["base"] |
#bitfield_type(arch_def, effective_xlen = nil) ⇒ Idl::BitfieldType
Returns A bitfield type that can represent all fields of the CSR.
397 398 399 400 401 402 403 404 |
# File 'lib/arch_obj_models/csr.rb', line 397 def bitfield_type(arch_def, effective_xlen = nil) Idl::BitfieldType.new( "Csr#{name.capitalize}Bitfield", length(arch_def, effective_xlen), fields_for(effective_xlen).map(&:name), fields_for(effective_xlen).map { |f| f.location(arch_def, effective_xlen) } ) end |
#defined_in_all_bases? ⇒ Boolean
Returns true if this CSR is defined regardless of the effective XLEN.
48 |
# File 'lib/arch_obj_models/csr.rb', line 48 def defined_in_all_bases? = @data["base"].nil? |
#defined_in_base32? ⇒ Boolean
Returns true if this CSR is defined when XLEN is 32.
42 |
# File 'lib/arch_obj_models/csr.rb', line 42 def defined_in_base32? = @data["base"].nil? || @data["base"] == 32 |
#defined_in_base64? ⇒ Boolean
Returns true if this CSR is defined when XLEN is 64.
45 |
# File 'lib/arch_obj_models/csr.rb', line 45 def defined_in_base64? = @data["base"].nil? || @data["base"] == 64 |
#description_html ⇒ String
parse description field with asciidoctor, and return the HTML result
319 320 321 |
# File 'lib/arch_obj_models/csr.rb', line 319 def description_html Asciidoctor.convert description end |
#dynamic_length?(arch_def) ⇒ Boolean
Returns Whether or not the length of the CSR depends on a runtime value (e.g., mstatus.SXL).
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/arch_obj_models/csr.rb', line 111 def dynamic_length?(arch_def) return false if @data["length"].is_a?(Integer) # when a CSR is only defined in one base, its length can't change return false unless @data["base"].nil? case @data["length"] when "MXLEN" # mxlen can never change at runtime, so if we have it in the config, the length is not dynamic # if we don't have it in the config, we don't know what the length is return arch_def.mxlen.nil? when "SXLEN" # dynamic if either we don't know SXLEN or SXLEN is explicitly mutable [nil, 3264].include?(arch_def.param_values["SXLEN"]) when "VSXLEN" # dynamic if either we don't know VSXLEN or VSXLEN is explicitly mutable [nil, 3264].include?(arch_def.param_values["VSXLEN"]) else raise "Unexpected length" end end |
#exists_in_cfg?(arch_def) ⇒ Boolean
Returns whether or not the CSR is possibly implemented given the supplies config options.
557 558 559 560 561 562 563 564 565 |
# File 'lib/arch_obj_models/csr.rb', line 557 def exists_in_cfg?(arch_def) if arch_def.fully_configured? (@data["base"].nil? || (arch_def.possible_xlens.include? @data["base"])) && arch_def.implemented_extensions.any? { |e| defined_by?(e) } else (@data["base"].nil? || (arch_def.possible_xlens.include? @data["base"])) && arch_def.prohibited_extensions.none? { |e| defined_by?(e) } end end |
#field(field_name) ⇒ Object
returns [CsrField,nil] field named ‘field_name’ if it exists, and nil otherwise
390 391 392 |
# File 'lib/arch_obj_models/csr.rb', line 390 def field(field_name) field_hash[field_name.to_s] end |
#field?(field_name) ⇒ Boolean
Returns true if a field named ‘field_name’ is defined in the csr, and false otherwise.
385 386 387 |
# File 'lib/arch_obj_models/csr.rb', line 385 def field?(field_name) field_hash.key?(field_name.to_s) end |
#field_hash ⇒ Hash<String,CsrField>
Returns Hash of fields, indexed by field name.
373 374 375 376 377 378 379 380 381 382 |
# File 'lib/arch_obj_models/csr.rb', line 373 def field_hash @field_hash unless @field_hash.nil? @field_hash = {} fields.each do |field| @field_hash[field.name] = field end @field_hash end |
#fields ⇒ Array<CsrField>
Returns All known fields of this CSR.
360 361 362 363 364 |
# File 'lib/arch_obj_models/csr.rb', line 360 def fields return @fields unless @fields.nil? @fields = @data["fields"].map { |_field_name, field_data| CsrField.new(self, field_data) } end |
#fields_for(effective_xlen) ⇒ Array<CsrField>
equivalent to #fields if effective_xlen
is nil
368 369 370 |
# File 'lib/arch_obj_models/csr.rb', line 368 def fields_for(effective_xlen) fields.select { |f| effective_xlen.nil? || !f.key?("base") || f.base == effective_xlen } end |
#format_changes_with_xlen?(arch_def) ⇒ Boolean
Returns Whether or not the format of this CSR changes when the effective XLEN changes in some mode.
52 53 54 55 56 57 |
# File 'lib/arch_obj_models/csr.rb', line 52 def format_changes_with_xlen?(arch_def) dynamic_length?(arch_def) || implemented_fields(arch_def).any? do |f| f.dynamic_location?(arch_def) end end |
#has_custom_sw_read? ⇒ Boolean
Returns true if the CSR has a custom sw_read function.
407 408 409 |
# File 'lib/arch_obj_models/csr.rb', line 407 def has_custom_sw_read? @data.key?("sw_read()") && !@data["sw_read()"].empty? end |
#implemented_fields(arch_def) ⇒ Array<CsrField>
Returns All implemented fields for this CSR Excluded any fields that are defined by unimplemented extensions.
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 |
# File 'lib/arch_obj_models/csr.rb', line 341 def implemented_fields(arch_def) return @implemented_fields unless @implemented_fields.nil? implemented_bases = if arch_def.param_values["SXLEN"] == 3264 || arch_def.param_values["UXLEN"] == 3264 || arch_def.param_values["VSXLEN"] == 3264 || arch_def.param_values["VUXLEN"] == 3264 [32, 64] else [arch_def.param_values["XLEN"]] end @implemented_fields = fields.select do |f| f.exists_in_cfg?(arch_def) end end |
#implemented_fields_for(arch_def, effective_xlen) ⇒ Array<CsrField>
Returns All implemented fields for this CSR at the given effective XLEN, sorted by location (smallest location first) Excluded any fields that are defined by unimplemented extensions or a base that is not effective_xlen.
326 327 328 329 330 331 332 333 334 335 336 |
# File 'lib/arch_obj_models/csr.rb', line 326 def implemented_fields_for(arch_def, effective_xlen) @implemented_fields_for ||= {} key = [arch_def.name, effective_xlen].hash return @implemented_fields_for[key] if @implemented_fields_for.key?(key) @implemented_fields_for[key] = implemented_fields(arch_def).select do |f| !f.key?("base") || f.base == effective_xlen end end |
#length(arch_def, effective_xlen = nil) ⇒ Integer?
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 |
# File 'lib/arch_obj_models/csr.rb', line 163 def length(arch_def, effective_xlen = nil) case @data["length"] when "MXLEN" return arch_def.mxlen unless arch_def.mxlen.nil? if !@data["base"].nil? @data["base"] else # don't know MXLEN effective_xlen end when "SXLEN" if arch_def.param_values.key?("SXLEN") if arch_def.param_values["SXLEN"] == 3264 effective_xlen else arch_def.param_values["SXLEN"] end elsif !@data["base"].nil? # if this CSR is only available in one base, then we know its length @data["base"] else # don't know SXLEN effective_xlen end when "VSXLEN" if arch_def.param_values.key?("VSXLEN") if arch_def.param_values["VSXLEN"] == 3264 effective_xlen else arch_def.param_values["VSXLEN"] end elsif !@data["base"].nil? # if this CSR is only available in one base, then we know its length @data["base"] else # don't know VSXLEN effective_xlen end when Integer @data["length"] else raise "Unexpected length field for #{csr.name}" end end |
#length_cond32 ⇒ String
Returns IDL condition of when the effective xlen is 32.
244 245 246 247 248 249 250 251 252 253 254 255 |
# File 'lib/arch_obj_models/csr.rb', line 244 def length_cond32 case @data["length"] when "MXLEN" "CSR[misa].MXL == 0" when "SXLEN" "CSR[mstatus].SXL == 0" when "VSXLEN" "CSR[hstatus].VSXL == 0" else raise "Unexpected length #{@data['length']} for #{name}" end end |
#length_cond64 ⇒ String
Returns IDL condition of when the effective xlen is 64.
258 259 260 261 262 263 264 265 266 267 268 269 |
# File 'lib/arch_obj_models/csr.rb', line 258 def length_cond64 case @data["length"] when "MXLEN" "CSR[misa].MXL == 1" when "SXLEN" "CSR[mstatus].SXL == 1" when "VSXLEN" "CSR[hstatus].VSXL == 1" else raise "Unexpected length" end end |
#length_pretty(arch_def, effective_xlen = nil) ⇒ String
Returns Pretty-printed length string.
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 |
# File 'lib/arch_obj_models/csr.rb', line 273 def length_pretty(arch_def, effective_xlen=nil) if dynamic_length?(arch_def) cond = case @data["length"] when "MXLEN" "CSR[misa].MXL == %%" when "SXLEN" "CSR[mstatus].SXL == %%" when "VSXLEN" "CSR[hstatus].VSXL == %%" else raise "Unexpected length '#{@data['length']}'" end if effective_xlen.nil? <<~LENGTH #{length(arch_def, 32)} when #{cond.sub('%%', '0')} #{length(arch_def, 64)} when #{cond.sub('%%', '1')} LENGTH else "#{length(arch_def, effective_xlen)}-bit" end else "#{length(arch_def)}-bit" end end |
#long_name ⇒ Object
27 28 29 |
# File 'lib/arch_obj_models/csr.rb', line 27 def long_name @data["long_name"] end |
#max_length(arch_def) ⇒ Integer
Returns The largest length of this CSR in any valid mode/xlen for the config.
148 149 150 151 152 153 154 155 156 157 |
# File 'lib/arch_obj_models/csr.rb', line 148 def max_length(arch_def) case @data["length"] when "MXLEN", "SXLEN", "VSXLEN" 64 when Integer @data["length"] else raise "Unexpected length" end end |
#min_length(arch_def) ⇒ Integer
Returns Smallest length of the CSR in any mode.
135 136 137 138 139 140 141 142 143 144 |
# File 'lib/arch_obj_models/csr.rb', line 135 def min_length(arch_def) case @data["length"] when "MXLEN", "SXLEN", "VSXLEN" 32 when Integer @data["length"] else raise "Unexpected length" end end |
#modes_with_access ⇒ Object
list of modes that can potentially access the field
301 302 303 304 305 306 307 308 309 310 311 312 313 314 |
# File 'lib/arch_obj_models/csr.rb', line 301 def modes_with_access case @data["priv_mode"] when "M" ["M"] when "S" ["M", "S", "VS"] when "U" ["M", "S", "U", "VS", "VU"] when "VS" ["M", "S", "VS"] else raise "unexpected priv mode" end end |
#optional_in_cfg?(arch_def) ⇒ Boolean
Returns whether or not the CSR is optional in the config.
569 570 571 572 573 574 575 576 577 578 |
# File 'lib/arch_obj_models/csr.rb', line 569 def optional_in_cfg?(arch_def) raise "optional_in_cfg? should only be used by a partially-specified arch def" unless arch_def.partially_configured? exists_in_cfg?(arch_def) && arch_def.mandatory_extensions.all? do |ext_req| ext_req.(arch_def).none? do |ext_ver| defined_by?(ext_ver) end end end |
#priv_mode ⇒ String
Returns Least-privileged mode that can access this CSR. One of [‘m’, ‘s’, ‘u’, ‘vs’, ‘vu’].
23 24 25 |
# File 'lib/arch_obj_models/csr.rb', line 23 def priv_mode @data["priv_mode"] end |
#pruned_sw_read_ast(arch_def) ⇒ Object
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 |
# File 'lib/arch_obj_models/csr.rb', line 467 def pruned_sw_read_ast(arch_def) @pruned_sw_read_asts ||= {} ast = @pruned_sw_read_asts[arch_def.name] return ast unless ast.nil? ast = type_checked_sw_read_ast(arch_def.symtab) symtab = arch_def.symtab.global_clone symtab.push(ast) # all CSR instructions are 32-bit symtab.add( "__instruction_encoding_size", Idl::Var.new("__instruction_encoding_size", Idl::Type.new(:bits, width: 6), 32) ) symtab.add( "__expected_return_type", Idl::Type.new(:bits, width: 128) ) ast = ast.prune(symtab) ast.freeze_tree(arch_def.symtab) arch_def.idl_compiler.type_check( ast, symtab, "CSR[#{name}].sw_read()" ) symtab.pop symtab.release @pruned_sw_read_asts[arch_def.name] = ast end |
#reachable_functions(arch_def) ⇒ Array<Idl::FunctionDefAst>
Returns List of functions reachable from this CSR’s sw_read or a field’s sw_wirte function.
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 |
# File 'lib/arch_obj_models/csr.rb', line 61 def reachable_functions(arch_def) return @reachable_functions unless @reachable_functions.nil? fns = [] if has_custom_sw_read? ast = pruned_sw_read_ast(arch_def) symtab = arch_def.symtab.deep_clone symtab.push(ast) fns.concat(ast.reachable_functions(symtab)) end if arch_def.multi_xlen? implemented_fields_for(arch_def, 32).each do |field| fns.concat(field.reachable_functions(arch_def, 32)) end implemented_fields_for(arch_def, 64).each do |field| fns.concat(field.reachable_functions(arch_def, 64)) end else implemented_fields_for(arch_def, arch_def.mxlen).each do |field| fns.concat(field.reachable_functions(arch_def, arch_def.mxlen)) end end @reachable_functions = fns.uniq end |
#reachable_functions_unevaluated(arch_def) ⇒ Array<Idl::FunctionDefAst>
Returns List of functions reachable from this CSR’s sw_read or a field’s sw_wirte function, irrespective of context.
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/arch_obj_models/csr.rb', line 91 def reachable_functions_unevaluated(arch_def) return @reachable_functions_unevaluated unless @reachable_functions_unevaluated.nil? fns = [] if has_custom_sw_read? ast = sw_read_ast(arch_def) fns.concat(ast.reachable_functions_unevaluated(arch_def)) end fields.each do |field| fns.concat(field.reachable_functions_unevaluated(arch_def)) end @reachable_functions_unevaluated = fns.uniq end |
#sw_read_ast(symtab) ⇒ FunctionBodyAst
Returns The abstract syntax tree of the sw_read() function.
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 |
# File 'lib/arch_obj_models/csr.rb', line 443 def sw_read_ast(symtab) raise ArgumentError, "Argument should be a symtab" unless symtab.is_a?(Idl::SymbolTable) return @sw_read_ast unless @sw_read_ast.nil? return nil if @data["sw_read()"].nil? # now, parse the function @sw_read_ast = symtab.archdef.idl_compiler.compile_func_body( @data["sw_read()"], return_type: Idl::Type.new(:bits, width: 128), # big int to hold special return values name: "CSR[#{name}].sw_read()", input_file: __source, input_line: source_line("sw_read()"), symtab:, type_check: false ) raise "unexpected #{@sw_read_ast.class}" unless @sw_read_ast.is_a?(Idl::FunctionBodyAst) @sw_read_ast.set_input_file_unless_already_set(__source, source_line("sw_read()")) @sw_read_ast end |
#type_checked_sw_read_ast(symtab) ⇒ Object
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 |
# File 'lib/arch_obj_models/csr.rb', line 412 def type_checked_sw_read_ast(symtab) @type_checked_sw_read_asts ||= {} ast = @type_checked_sw_read_asts[symtab.hash] return ast unless ast.nil? symtab_hash = symtab.hash symtab = symtab.global_clone symtab.push(ast) # all CSR instructions are 32-bit symtab.add( "__instruction_encoding_size", Idl::Var.new("__instruction_encoding_size", Idl::Type.new(:bits, width: 6), 32) ) symtab.add( "__expected_return_type", Idl::Type.new(:bits, width: 128) ) ast = sw_read_ast(symtab) symtab.archdef.idl_compiler.type_check( ast, symtab, "CSR[#{name}].sw_read()" ) symtab.pop symtab.release @type_checked_sw_read_asts[symtab_hash] = ast end |
#virtual_address ⇒ Integer?
33 34 35 |
# File 'lib/arch_obj_models/csr.rb', line 33 def virtual_address @data["virtual_address"] end |
#wavedrom_desc(arch_def, effective_xlen, exclude_unimplemented: false, optional_type: 2) ⇒ Hash
Returns A representation of the WaveDrom drawing for the CSR (should be turned into JSON for wavedrom).
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 |
# File 'lib/arch_obj_models/csr.rb', line 515 def wavedrom_desc(arch_def, effective_xlen, exclude_unimplemented: false, optional_type: 2) desc = { "reg" => [] } last_idx = -1 field_list = if exclude_unimplemented implemented_fields_for(arch_def, effective_xlen) else fields_for(effective_xlen) end field_list.sort! { |a, b| a.location(arch_def, effective_xlen).min <=> b.location(arch_def, effective_xlen).min } field_list.each do |field| if field.location(arch_def, effective_xlen).min != last_idx + 1 # have some reserved space n = field.location(arch_def, effective_xlen).min - last_idx - 1 raise "negative reserved space? #{n} #{name} #{field.location(arch_def, effective_xlen).min} #{last_idx + 1}" if n <= 0 desc["reg"] << { "bits" => n, type: 1 } end if arch_def.partially_configured? && field.optional_in_cfg?(arch_def) desc["reg"] << { "bits" => field.location(arch_def, effective_xlen).size, "name" => field.name, type: optional_type } else desc["reg"] << { "bits" => field.location(arch_def, effective_xlen).size, "name" => field.name, type: 3 } end last_idx = field.location(arch_def, effective_xlen).max end if !field_list.empty? && (field_list.last.location(arch_def, effective_xlen).max != (length(arch_def, effective_xlen) - 1)) # reserved space at the end desc["reg"] << { "bits" => (length(arch_def, effective_xlen) - 1 - last_idx), type: 1 } # desc['reg'] << { 'bits' => 1, type: 1 } end desc["config"] = { "bits" => length(arch_def, effective_xlen) } desc["config"]["lanes"] = length(arch_def, effective_xlen) / 16 desc end |