cpclib_asm/assembler/
section.rs

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
65
66
67
68
69
70
71
72
73
use crate::{Env, ExprResult};

#[derive(Clone, Debug)]
pub struct Section {
    /// Name of the section
    pub(crate) name: String,
    /// Start address of the section
    pub(crate) start: u16,
    /// Last (included) address of the section
    pub(crate) stop: u16,
    /// Expected mmr configuration
    pub(crate) mmr: u8,

    pub(crate) output_adr: u16,
    pub(crate) max_output_adr: u16,
    pub(crate) code_adr: u16
}

impl Section {
    pub(crate) fn new(name: &str, start: u16, stop: u16, mmr: u8) -> Self {
        Section {
            mmr,
            name: name.to_owned(),
            start,
            stop,

            output_adr: start,
            code_adr: start,

            max_output_adr: start
        }
    }

    pub fn contains(&self, addr: u16) -> bool {
        addr >= self.start && addr <= self.stop
    }

    pub(crate) fn new_pass(&mut self) {
        self.output_adr = self.start;
        self.code_adr = self.start;
    }

    pub fn length(&self) -> u16 {
        self.stop - self.start + 1
    }

    pub fn used(&self) -> u16 {
        self.max_output_adr - self.start
    }
}

/// Returns the address of the beginning of the section
pub fn section_start(section_name: &str, env: &Env) -> Result<ExprResult, crate::AssemblerError> {
    Ok(env.get_section_description(section_name)?.start.into())
}

pub fn section_stop(section_name: &str, env: &Env) -> Result<ExprResult, crate::AssemblerError> {
    Ok(env.get_section_description(section_name)?.stop.into())
}

/// Returns the number of bytes available  in the section
pub fn section_length(section_name: &str, env: &Env) -> Result<ExprResult, crate::AssemblerError> {
    Ok(env.get_section_description(section_name)?.length().into())
}

/// Returns the number of bytes consummed in the section
pub fn section_used(section_name: &str, env: &Env) -> Result<ExprResult, crate::AssemblerError> {
    Ok(env.get_section_description(section_name)?.used().into())
}

pub fn section_mmr(section_name: &str, env: &Env) -> Result<ExprResult, crate::AssemblerError> {
    Ok(env.get_section_description(section_name)?.mmr.into())
}