Utils

utils.py

created by Eduardo Diaz

This module defines all the common utils needed in order to make programming easier

armsim.utils.asr(val, x, n=32)
Executes an arithmetic shift right of x bits on number val, of n bits length
armsim.utils.bitsetRangeToNum(bitset)

Returns a bitset range converted to a number

Usage:

bitsetRangeToNum(bitset[5:8])
armsim.utils.checkCondition(processor, inst_val)
Checks if an instruction should be executed according to first 4 bits of the instruction
armsim.utils.countSetBits(num)

Returns number of bits set to one

Example:

countSetBits(5) #returns 2 (0b0101), 2 bits set to 1
countSetBits(1) #returns 1 (0b0001), 1 bit set to 1
armsim.utils.getBits(num, bit, num_bits=1, size=32)

Returns the value of a bit or multiple bits of num.

Example:

num = 0b10001101
getBits(num, 0)    # returns 1
getBits(num, 1)    # returns 0
getBits(num, 4, 4) # returns 8 (0b1000)
getBits(num, 0, 8) # returns 141 (0b10001101)
armsim.utils.itoa(x, base=10)
Converts and integer value to its string representation in base base
armsim.utils.rol(val, n, x)
Executes a rotate left of x bits on number val, of n bits length
armsim.utils.ror(val, n, x)
Executes a rotate right of x bits on number val, of n bits length
armsim.utils.setBits(num, bit, value, num_bits=1, size=32)

Sets and returns the value of a bit or multiple bits of num.

Example:

num = 0b10001101 (141)
setBits(num, 0, 0)    # returns 140 (0b10001100)
setBits(num, 1, 1)    # returns 143 (0b10001111)
setBits(num, 4, 5, 4) # returns 221 (0b11011101)
armsim.utils.signExtend(num, bits, newBits)

Extends a number from bits bits to newBits bits

Example:

num = 0b0011
signExtend(num, 4, 10) # returns 0b0000000011
num = 0b1011
signExtend(num, 4, 10) # returns 0b1111111011
armsim.utils.signValue(num, bits=32)

Returns signed value of num.

if msb equals zero, it returns num.

if msb equals one, it returns negative two’s complement of num.

armsim.utils.testInstruction(inst, inst_val)
Function to check if an instruction matches the interface’s bitmask

Previous topic

Implementing a new instruction

Next topic

Contribute

This Page