1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//! Instructions related to Falcon virtual memory in the code segment.

use faucon_asm::Instruction;

use super::Cpu;

/// Reads the TLB corresponding to a given physical address.
pub fn ptlb(cpu: &mut Cpu, insn: &Instruction) -> usize {
    let operands = insn.operands();

    // Extract the instruction operands (two registers).
    let destination = operands[0];
    let source = operands[1];

    // Look up the TLB.
    let physical_index = ((cpu.registers[source] & 0xFF) << 8) as u16;
    let tlb = cpu.memory.tlb.get_physical_entry(physical_index);

    // Build and write the result value.
    cpu.registers[destination] = (tlb.flags as u32) << 24 | (tlb.virtual_page_number as u32) << 8;

    // Signal regular PC increment to the CPU.
    cpu.increment_pc = true;

    1
}

/// Reads the TLB corresponding to a given virtual address.
pub fn vtlb(cpu: &mut Cpu, insn: &Instruction) -> usize {
    let operands = insn.operands();

    // Extract the instruction operands (two registers).
    let destination = operands[0];
    let source = operands[1];

    // Look up the TLB to get the result value and write it to the destination.
    let result = cpu.memory.tlb.lookup_raw(cpu.registers[source] & 0xFFFFFF);
    cpu.registers[destination] = result;

    // Signal regular PC increment to the CPU.
    cpu.increment_pc = true;

    1
}

/// Invalidates a TLB entry corresponding to a physical address.
pub fn itlb(cpu: &mut Cpu, insn: &Instruction) -> usize {
    let operands = insn.operands();

    // Extract the instruction operand (one register).
    let source = operands[0];

    // Look up the TLB.
    let physical_index = ((cpu.registers[source] & 0xFF) << 8) as u16;
    let tlb = cpu.memory.tlb.get_physical_entry(physical_index);

    // Clear the entry.
    tlb.clear();

    // Signal regular PC increment to the CPU.
    cpu.increment_pc = true;

    1
}