Skip to content

Commit

Permalink
feat: added Ord and PartialOrd traits on all structs and removed copy…
Browse files Browse the repository at this point in the history
… semantic (#13)
  • Loading branch information
standard3 committed Jun 24, 2024
1 parent cc0895e commit 4435393
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
18 changes: 9 additions & 9 deletions src/architecture/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ use std::{hash, path::PathBuf};
use super::riscv::MMUMode as RiscVMMUMode;

/// Enumerates types of memory regions.
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Ord, PartialOrd, Hash, Default)]
pub enum MemoryRegionType {
#[default]
RAM,
ROM,
}

/// Represents a memory region with a start and end address.
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Ord, PartialOrd, Hash, Default)]
pub struct MemoryRegion {
pub region_type: MemoryRegionType,
pub start_address: u64,
Expand Down Expand Up @@ -65,7 +65,7 @@ impl MemoryRegion {
}

/// Represents a memory space with regions.
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Hash, Default)]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Ord, PartialOrd, Hash, Default)]
pub struct MemorySpace {
pub regions: Vec<MemoryRegion>,
}
Expand Down Expand Up @@ -93,7 +93,7 @@ impl MemorySpace {
/// Represents a CPU register with a value.
/// Depending on the architecture, the *validity* changes.
pub trait CPURegister {
type Value: hash::Hash + Eq + Copy + Default;
type Value: hash::Hash + Eq + Default;

fn is_valid(&self) -> Result<Self::Value>;
}
Expand All @@ -103,8 +103,8 @@ pub trait CPURegister {
/// There is also auxiliary information about the page such as a present bit, a dirty or modified bit,
/// address space or process ID information, amongst others.
pub trait PageTableEntry {
type Address: hash::Hash + Eq + Copy + Default;
type Flags: hash::Hash + Eq + Copy + Default;
type Address: hash::Hash + Eq + Default;
type Flags: hash::Hash + Eq + Default;

fn is_dirty(&self) -> bool;
fn is_accessed(&self) -> bool;
Expand All @@ -119,14 +119,14 @@ pub trait PageTableEntry {
/// It is used to translate virtual addresses to physical addresses and to manage the memory permissions of the pages.
/// It is also used to store additional information about the pages, such as the status of the page, the address space or process ID, amongst others.
pub trait PageTable {
type Entries: hash::Hash + Eq + Copy + Default + PageTableEntry;
type Entries: hash::Hash + Eq + Default + PageTableEntry;

// fn apply_on_entries(function: FnMut(PageTableEntry) -> Vec<?> ) -> ? // FIXME: to be defined, but is it necessary?
}

/// Enumerates types of supported machines.
/// This enum is used to specify the type of machine that is being parsed.
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Ord, PartialOrd, Hash)]
pub enum MachineType {
RiscV(RiscVMMUMode),
}
Expand All @@ -139,7 +139,7 @@ impl Default for MachineType {

/// Represents a machine with a type, MMU, CPU, memory regions, and an associated dump file.
/// It is used to store the machine's configuration, memory regions, and the dump file that is being used.
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Hash, Default)]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Ord, PartialOrd, Hash, Default)]
pub struct Machine {
/// Type of the machine and its associated MMU mode.
pub machine_type: MachineType,
Expand Down
10 changes: 5 additions & 5 deletions src/architecture/riscv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use anyhow::Result;
use serde::{Deserialize, Serialize};

/// Represents a RISC-V CPU register associated with a value.
#[derive(Debug, Clone, Copy, Serialize, Deserialize, Default, Hash, Eq, PartialEq)]
#[derive(Debug, Clone, Serialize, Deserialize, Default, Hash, Eq, PartialEq, Ord, PartialOrd)]
pub struct CPURegister {
pub value: u64,
}
Expand All @@ -27,7 +27,7 @@ impl CPURegister {
/// It holds the mapping between a virtual address of a page and the address of a physical frame.
/// There is also auxiliary information about the page such as a present bit, a dirty or modified bit,
/// address space or process ID information, amongst others.
#[derive(Debug, Clone, Copy, Serialize, Deserialize, Hash, Eq, PartialEq)]
#[derive(Debug, Clone, Serialize, Deserialize, Hash, Eq, PartialEq, Ord, PartialOrd)]
pub struct PageTableEntry {
pub address: u64,
pub flags: u64,
Expand Down Expand Up @@ -76,7 +76,7 @@ impl PageTableEntryTrait for PageTableEntry {
/// Enumerates RISC-V MMU modes.
/// The MMU modes are used to determine the number of bits used for virtual and physical addresses.
/// The modes are named after the number of bits used for the virtual address space.
#[derive(Debug, Clone, Copy, Serialize, Deserialize, Hash, Eq, PartialEq, Default)]
#[derive(Debug, Clone, Serialize, Deserialize, Hash, Eq, PartialEq, Ord, PartialOrd, Default)]
pub enum MMUMode {
#[default]
SV32,
Expand All @@ -85,7 +85,7 @@ pub enum MMUMode {
}

/// Represents a RISC-V CPU.
#[derive(Debug, Clone, Serialize, Deserialize, Default, Hash, Eq, PartialEq)]
#[derive(Debug, Clone, Serialize, Deserialize, Default, Hash, Eq, PartialEq, Ord, PartialOrd)]
pub struct CPU {
pub registers: Vec<CPURegister>,
}
Expand All @@ -99,7 +99,7 @@ impl CPU {
}

/// Represents a RISC-V MMU.
#[derive(Debug, Clone, Serialize, Deserialize, Default, Hash, Eq, PartialEq)]
#[derive(Debug, Clone, Serialize, Deserialize, Default, Hash, Eq, PartialEq, Ord, PartialOrd)]
pub struct MMU {
pub mode: MMUMode,
}
Expand Down

0 comments on commit 4435393

Please sign in to comment.