Class CsrBitSet
- java.lang.Object
-
- java.util.BitSet
-
- cbsw.CsrBitSet
-
- All Implemented Interfaces:
java.io.Serializable
,java.lang.Cloneable
public class CsrBitSet extends java.util.BitSet implements java.lang.Cloneable
Bit-oriented register descriptor collection
Hardware interfaces are typically manipulated through a set of addressable byte-oriented registers. This class is intended to simplify bit-oriented access to such registers. It encapsulates a collection of descriptors for fields containing consecutive bits. Each field is assigned a key, a bit offset from the start of the register array and the length of consecutive bits belonging to the field. Internally, the class maintains a copy of the current register settings as a BitSet. Application code can manipulate the contents of the cache using get() and set() methods. Writing the hardware is achieved through the put() methods using the RegisterIo interface.
The class unmarshalls the collection of bitfields from an XML file. An example bit-field descriptor might appear like this in the XML:
<register name="gt.0.precursor" offset="536" length="5">GT TX precursor value</register>
The name attribute is used as the HashMap key.
Several of the methods are generic. Where the generic vaue represents a bit-field value, the types Integer, Boolean or String are allowed. All others result in the method throwing a RuntimeException.
To support triplicated registers (such as used by the GBTX ASIC), the offset attribute may be a comma-separated list of (usually 3) offsets. The bit-field values are replicated to each offset in the list.
- See Also:
RegisterDescriptor
,RegisterIo
, Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description (package private) int
csrSize
(package private) java.util.ArrayList<RegisterDescriptor>
descriptors
(package private) java.util.HashMap<java.lang.String,RegisterDescriptor>
map
(package private) java.util.BitSet
roBits
-
Constructor Summary
Constructors Constructor Description CsrBitSet(int csrSize, java.util.ArrayList<RegisterDescriptor> descriptors, java.util.HashMap<java.lang.String,RegisterDescriptor> map)
CsrBitSet(int csrSize, java.util.ArrayList<RegisterDescriptor> descriptors, java.util.HashMap<java.lang.String,RegisterDescriptor> map, java.io.File cf)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description java.lang.Object
clone()
void
decode()
Displays the values of all bit-fields on System.outvoid
decode(java.util.BitSet bits)
Displays the values of all bit-fields on System.outjava.lang.String
get(java.lang.String key)
Gets hex string representation of bit-fieldjava.lang.String
get(java.lang.String key, byte[] regs)
Gets hex string representation of bit-fieldjava.lang.String
get(java.lang.String key, java.util.BitSet bits)
Gets hex string representation of bit-fieldbyte[]
getBytes(java.lang.String key, int arrayOffset)
Deprecated.int
getCsrSize()
Returns the CSR sizejava.util.ArrayList<RegisterDescriptor>
getDescriptors()
Returns the bit-field descriptor listjava.util.HashMap<java.lang.String,RegisterDescriptor>
getMap()
Returns the descriptor map(package private) static java.lang.String
getPrefsValue(java.lang.String key, java.util.prefs.Preferences prefs, java.lang.Object defaultValue)
Converts a value from a Preference into String representationRegisterDescriptor
getRegisterDescriptor(java.lang.String key)
Returns the RegisterDescriptor for the given keyjava.util.BitSet
getRoBits()
void
saveToBinaryFile()
void
saveToBinaryFile(java.io.File file)
void
set(byte[] buf)
Initialises the CsrBitSet from a byte arrayvoid
set(java.io.File cf)
Initialises the CsrBitSet from binary filevoid
set(java.io.File cf, int skip)
Initialises the CsrBitSet from a binary file.void
set(java.io.FileReader fr)
Read the register settings from file and update CsrBitSet.<T> void
set(java.lang.String key, java.util.prefs.Preferences prefs, T defaultValue)
Sets the register bits from value stored in Java Preference<T> void
set(java.lang.String key, T value)
Sets bit-fieldvoid
setRegex(java.lang.String key, java.lang.String val)
Writes the value to all register bit-fields matching a regular expression.java.util.stream.Stream<RegisterDescriptor>
stream(java.lang.String regex)
Get a stream of registers whose name matches the supplied regexjava.util.stream.Stream<RegisterDescriptor>
stream(java.lang.String regex, java.lang.String value)
Get a stream of registers whose name matches the supplied regex and content matches the supplied value.java.util.stream.Stream<RegisterDescriptor>
streamComplement(java.lang.String regex, java.lang.String value)
Get a stream of registers whose name matches the supplied regex and content does not match the supplied value.java.util.stream.Stream<RegisterDescriptor>
streamRegisterDescriptors()
Returns a Collection stream.byte[]
toByteArray(int arrayOffset)
Returns a new byte array containing the register valuesstatic java.lang.String
valueOf(java.lang.Object obj)
Converts value into String representation-
Methods inherited from class java.util.BitSet
and, andNot, cardinality, clear, clear, clear, equals, flip, flip, get, get, hashCode, intersects, isEmpty, length, nextClearBit, nextSetBit, or, previousClearBit, previousSetBit, set, set, set, set, size, stream, toByteArray, toLongArray, toString, valueOf, valueOf, valueOf, valueOf, xor
-
-
-
-
Field Detail
-
csrSize
int csrSize
-
descriptors
java.util.ArrayList<RegisterDescriptor> descriptors
-
map
java.util.HashMap<java.lang.String,RegisterDescriptor> map
-
roBits
java.util.BitSet roBits
-
-
Constructor Detail
-
CsrBitSet
public CsrBitSet(int csrSize, java.util.ArrayList<RegisterDescriptor> descriptors, java.util.HashMap<java.lang.String,RegisterDescriptor> map)
-
CsrBitSet
public CsrBitSet(int csrSize, java.util.ArrayList<RegisterDescriptor> descriptors, java.util.HashMap<java.lang.String,RegisterDescriptor> map, java.io.File cf)
-
-
Method Detail
-
clone
public java.lang.Object clone()
- Overrides:
clone
in classjava.util.BitSet
-
getRoBits
public java.util.BitSet getRoBits()
-
getCsrSize
public int getCsrSize()
Returns the CSR size
- Returns:
- The size
-
getDescriptors
public java.util.ArrayList<RegisterDescriptor> getDescriptors()
Returns the bit-field descriptor list
- Returns:
- The list
-
getMap
public java.util.HashMap<java.lang.String,RegisterDescriptor> getMap()
Returns the descriptor map
- Returns:
- The map
-
streamRegisterDescriptors
public java.util.stream.Stream<RegisterDescriptor> streamRegisterDescriptors()
Returns a Collection stream.
- Returns:
- The stream
-
get
public java.lang.String get(java.lang.String key)
Gets hex string representation of bit-field
- Parameters:
key
- The bit-field key- Returns:
- String containg hex representation of bit-field
-
get
public java.lang.String get(java.lang.String key, java.util.BitSet bits)
Gets hex string representation of bit-field
This variant allows a bit-field to be retrieved from a BitSet that is not necessarily the internally cached version.
- Parameters:
key
- The bit-field keybits
- A BitSet containing the register values- Returns:
- String containg hex representation of bit-field
-
get
public java.lang.String get(java.lang.String key, byte[] regs)
Gets hex string representation of bit-field
This variant allows a bit-field to be retrieved from a byte array that is assumed to have the same shape as the register array described by this CsrBitSet.
- Parameters:
key
- The bit-field keyregs
- A byte array containing the register values- Returns:
- String containg hex representation of bit-field
-
set
public void set(java.io.File cf)
Initialises the CsrBitSet from binary file
The BitSet cache is initialised from the binary data. The previous content, is discarded.
- Parameters:
cf
- A File containing the binary data
-
set
public void set(java.io.File cf, int skip)
Initialises the CsrBitSet from a binary file.
- Parameters:
cf
- The configuration file.skip
- The number of csrSize blocks in the file to skip
-
set
public void set(java.io.FileReader fr)
Read the register settings from file and update CsrBitSet.
- Parameters:
fr
- The FileReader from where the values will be read
-
saveToBinaryFile
public void saveToBinaryFile(java.io.File file)
-
saveToBinaryFile
public void saveToBinaryFile()
-
set
public void set(byte[] buf)
Initialises the CsrBitSet from a byte array
The BitSet cache is initialised from the binary data. The previous content, is discarded.
- Parameters:
buf
- A byte array
-
valueOf
public static java.lang.String valueOf(java.lang.Object obj)
Converts value into String representation
Types String, Integer and Boolean are allowed. Throws a RuntimeException for any other type.
- Parameters:
obj
- The value- Returns:
- The hex String representation
-
getPrefsValue
static java.lang.String getPrefsValue(java.lang.String key, java.util.prefs.Preferences prefs, java.lang.Object defaultValue)
Converts a value from a Preference into String representation
Types String, Integer and Boolean are allowed. Throws a RuntimeException for any other type.
- Parameters:
key
- The key in the Preferences nodeprefs
- The Preferences nodedefaultValue
- The default value if the Preference does not exist- Returns:
- The hex String representation
-
set
public <T> void set(java.lang.String key, T value)
Sets bit-field
- Type Parameters:
T
- Type of the value to set- Parameters:
key
- The bit-field keyvalue
- The value to set
-
set
public <T> void set(java.lang.String key, java.util.prefs.Preferences prefs, T defaultValue)
Sets the register bits from value stored in Java Preference
This is a convenience method for when the bit-field should be set according to a value stored as a Java Preference.
key is both the XML key and the preference key name
The default value is applied if no Preference is found.
- Type Parameters:
T
- Type of the value to set- Parameters:
key
- The bit-field keyprefs
- The Preferences nodedefaultValue
- Default value
-
setRegex
public void setRegex(java.lang.String key, java.lang.String val)
Writes the value to all register bit-fields matching a regular expression.
- Parameters:
key
- The register bit-field key name regular expression.val
- The value to write.
-
stream
public java.util.stream.Stream<RegisterDescriptor> stream(java.lang.String regex)
Get a stream of registers whose name matches the supplied regex
- Parameters:
regex
- The pattern to match with the bit-field key- Returns:
- The stream
-
stream
public java.util.stream.Stream<RegisterDescriptor> stream(java.lang.String regex, java.lang.String value)
Get a stream of registers whose name matches the supplied regex and content matches the supplied value.
- Parameters:
regex
- The pattern to match with the bit-field keyvalue
- The value to match with the bit-field content- Returns:
- The stream
-
streamComplement
public java.util.stream.Stream<RegisterDescriptor> streamComplement(java.lang.String regex, java.lang.String value)
Get a stream of registers whose name matches the supplied regex and content does not match the supplied value.
- Parameters:
regex
- The pattern to match with the bit-field keyvalue
- The value to match with the bit-field content- Returns:
- The stream
-
toByteArray
public byte[] toByteArray(int arrayOffset)
Returns a new byte array containing the register values
The method creates an array of bytes containing the cached register values starting arrayOffset bytes from the start of the register array.
- Parameters:
arrayOffset
- Offset from register array start- Returns:
- The register values
-
getRegisterDescriptor
public RegisterDescriptor getRegisterDescriptor(java.lang.String key)
Returns the RegisterDescriptor for the given key
- Parameters:
key
- The bit-field key- Returns:
- The register descriptor
-
getBytes
@Deprecated public byte[] getBytes(java.lang.String key, int arrayOffset)
Deprecated.Returns a new byte array containing the value of the specified bit-field
The method creates an array of bytes containing the cached bit-field value. The value is offset arrayOffset bytes from the start of the returned array.
- Parameters:
key
- The bit-field keyarrayOffset
- Offset from register array start- Returns:
- The register values
-
decode
public void decode()
Displays the values of all bit-fields on System.out
-
decode
public void decode(java.util.BitSet bits)
Displays the values of all bit-fields on System.out
- Parameters:
bits
- The CSR bits.
-
-