Class: AVR::MemoryByteRegisterWithNamedBits
- Inherits:
-
MemoryByteRegister
- Object
- Value
- Register
- MemoryByteRegister
- AVR::MemoryByteRegisterWithNamedBits
- Defined in:
- lib/avr/register/memory_byte_register_with_named_bits.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#bit_names ⇒ Object
readonly
Returns the value of attribute bit_names.
Attributes inherited from MemoryByteRegister
Attributes inherited from Register
Attributes inherited from Value
Instance Method Summary collapse
- #bit_values ⇒ Object
- #diff_values(old_value, new_value) ⇒ Object
- #fetch(name) ⇒ Object
- #fetch_bit(name) ⇒ Object
- #from_h(hash) ⇒ Object
- #hash_for_value(value) ⇒ Object
-
#initialize(cpu, name, memory_byte, bit_names) ⇒ MemoryByteRegisterWithNamedBits
constructor
A new instance of MemoryByteRegisterWithNamedBits.
- #inspect ⇒ Object
- #mask_for_flags(flags) ⇒ Object
- #reset ⇒ Object
- #value_for_hash(hash) ⇒ Object
Methods inherited from MemoryByteRegister
Methods inherited from Value
#format, #to_i, #to_s, #value_hex
Constructor Details
#initialize(cpu, name, memory_byte, bit_names) ⇒ MemoryByteRegisterWithNamedBits
Returns a new instance of MemoryByteRegisterWithNamedBits.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/avr/register/memory_byte_register_with_named_bits.rb', line 8 def initialize(cpu, name, memory_byte, bit_names) super(cpu, name, memory_byte) @bit_names = bit_names @bit_names_bv = @bit_names.each_with_index.each_with_object({}) { |(b, i), h| h[b] = 2**i if b } @bit_names_bv.each do |bit_name, bit_value| define_singleton_method(bit_name, proc { (value & bit_value) == bit_value }) define_singleton_method((bit_name.to_s + '=').to_sym, proc { |new_value| if [true, 1].include?(new_value) self.value |= bit_value elsif [false, 0].include?(new_value) self.value &= ~bit_value else raise "Bad value #{new_value} for bit #{bit_name}" end }) end end |
Instance Attribute Details
#bit_names ⇒ Object (readonly)
Returns the value of attribute bit_names.
6 7 8 |
# File 'lib/avr/register/memory_byte_register_with_named_bits.rb', line 6 def bit_names @bit_names end |
Instance Method Details
#bit_values ⇒ Object
42 43 44 |
# File 'lib/avr/register/memory_byte_register_with_named_bits.rb', line 42 def bit_values @bit_names.reject(&:nil?).map { |name| name.to_s + '=' + (send(name) ? '1' : '0') }.join(', ') end |
#diff_values(old_value, new_value) ⇒ Object
74 75 76 77 78 79 80 81 82 83 |
# File 'lib/avr/register/memory_byte_register_with_named_bits.rb', line 74 def diff_values(old_value, new_value) diff_mask = old_value ^ new_value diff_strings = [] @bit_names_bv.each do |flag, mask| old_bit = (old_value & mask) != 0 ? 1 : 0 new_bit = (new_value & mask) != 0 ? 1 : 0 diff_strings << "#{flag}=#{old_bit}->#{new_bit}" if diff_mask & mask != 0 end '[' + diff_strings.join(', ') + ']' end |
#fetch(name) ⇒ Object
28 29 30 |
# File 'lib/avr/register/memory_byte_register_with_named_bits.rb', line 28 def fetch(name) send(name) end |
#fetch_bit(name) ⇒ Object
32 33 34 |
# File 'lib/avr/register/memory_byte_register_with_named_bits.rb', line 32 def fetch_bit(name) fetch(name) ? 1 : 0 end |
#from_h(hash) ⇒ Object
64 65 66 67 68 |
# File 'lib/avr/register/memory_byte_register_with_named_bits.rb', line 64 def from_h(hash) mask, sum = value_for_hash(hash) self.value = (value & ~mask) | sum self end |
#hash_for_value(value) ⇒ Object
46 47 48 |
# File 'lib/avr/register/memory_byte_register_with_named_bits.rb', line 46 def hash_for_value(value) @bit_names_bv.each_with_object({}) { |(name, bv), hash| hash[name] = (value & bv != 0) } end |
#inspect ⇒ Object
70 71 72 |
# File 'lib/avr/register/memory_byte_register_with_named_bits.rb', line 70 def inspect "#<#{self.class.name} #{bit_values}>" end |
#mask_for_flags(flags) ⇒ Object
36 37 38 39 40 |
# File 'lib/avr/register/memory_byte_register_with_named_bits.rb', line 36 def mask_for_flags(flags) mask = 0 flags.each { |flag| mask |= @bit_names_bv[flag] } mask end |
#reset ⇒ Object
60 61 62 |
# File 'lib/avr/register/memory_byte_register_with_named_bits.rb', line 60 def reset self.value = 0 end |
#value_for_hash(hash) ⇒ Object
50 51 52 53 54 55 56 57 58 |
# File 'lib/avr/register/memory_byte_register_with_named_bits.rb', line 50 def value_for_hash(hash) mask = 0 sum = 0 hash.each do |name, status| mask |= @bit_names_bv[name] sum |= @bit_names_bv[name] if [true, 1].include?(status) end [mask, sum] end |