mftb
Move from Time Base - 7C 00 00 E6
mftb

Instruction Syntax

Mnemonic Format Flags
mftb rD None

Instruction Encoding

0
1
1
1
1
1
D
D
D
D
D
0
0
0
0
0
0
0
0
0
0
1
0
1
0
0
1
1
1
1
1
0

Field Bits Description
Primary Opcode 0-5 011111 (0x1F)
rD 6-10 Destination register
Reserved 11-20 Should be zero
XO 21-30 0101001111 (0x29F)
Reserved 31 Should be zero

Operation

rD ← TB

The contents of the Time Base register are copied to general-purpose register rD. The Time Base is a 64-bit counter that increments at a fixed frequency, providing a high-resolution timing source.

Note: This instruction provides access to the processor's time base for timing measurements and performance analysis. The time base frequency is implementation-specific but typically runs at a multiple of the processor clock frequency.

Affected Registers

General Purpose Registers (GPRs)

(always)

Time Base (TB)

(read only)

Examples

Basic Time Measurement

# Measure time between two points
mftb r3                # Get start time
# ... perform operation to measure ...
mftb r4                # Get end time
subf r5, r3, r4       # Calculate elapsed time

Performance Timing

# Time a function call
mftb r6                # Start time
bl function_to_time    # Call function
mftb r7                # End time
subf r8, r6, r7       # Function execution time

Loop Timing

# Time a loop execution
mftb r9                # Start time
li r10, 1000          # Loop count

loop:
    # ... loop body ...
    addi r10, r10, -1  # Decrement counter
    cmpwi r10, 0       # Check if done
    bne loop           # Continue if not done

mftb r11               # End time
subf r12, r9, r11     # Total loop time

High-Resolution Delay

# Implement precise delay
mftb r13               # Get current time
li r14, 1000          # Delay count (in time base units)

delay_loop:
    mftb r15           # Get current time
    subf r16, r13, r15 # Calculate elapsed time
    cmpw r16, r14      # Compare with desired delay
    blt delay_loop     # Continue if not enough time passed

Benchmarking

# Benchmark multiple operations
mftb r17               # Start benchmark
# ... first operation ...
mftb r18               # Time after first operation
# ... second operation ...
mftb r19               # Time after second operation
# ... third operation ...
mftb r20               # End time

# Calculate individual operation times
subf r21, r17, r18    # First operation time
subf r22, r18, r19    # Second operation time
subf r23, r19, r20    # Third operation time

System Timing

# System timing for scheduling
mftb r24               # Get current system time
# Use for task scheduling or timeout calculations

Profiling

# Profile code sections
mftb r25               # Profile start time
# ... code section 1 ...
mftb r26               # Section 1 end time
# ... code section 2 ...
mftb r27               # Section 2 end time
# ... code section 3 ...
mftb r28               # Profile end time

# Calculate section times
subf r29, r25, r26    # Section 1 time
subf r30, r26, r27    # Section 2 time
subf r31, r27, r28    # Section 3 time

Timeout Implementation

# Implement timeout mechanism
mftb r3                # Start time
li r4, 5000           # Timeout value (in time base units)

timeout_loop:
    # ... check for condition ...
    mftb r5            # Current time
    subf r6, r3, r5    # Elapsed time
    cmpw r6, r4        # Compare with timeout
    bge timeout_expired # Branch if timeout expired
    b timeout_loop     # Continue checking

Real-Time Timing

# Real-time timing for periodic tasks
mftb r7                # Get current time
# Calculate next execution time
addi r8, r7, 1000     # Next execution in 1000 time base units

# Wait until next execution time
wait_loop:
    mftb r9            # Current time
    cmpw r9, r8        # Check if time to execute
    blt wait_loop      # Wait if not yet time
    # Execute periodic task

Performance Monitoring

# Monitor performance over time
mftb r10               # Initial time
li r11, 0             # Performance counter

monitor_loop:
    # ... monitor operation ...
    addi r11, r11, 1   # Increment counter
    mftb r12           # Current time
    subf r13, r10, r12 # Total elapsed time
    
    # Check if monitoring period complete
    cmpwi r13, 10000   # 10,000 time base units
    blt monitor_loop   # Continue if not complete
    
    # Calculate performance metrics
    # r11 contains operation count
    # r13 contains total time

Related Instructions

mfspr, mtspr, mfcr, mtcrf

Back to Index