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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
 * File: body.rs
 * Project: cpu
 * Created Date: 02/05/2022
 * Author: Shun Suzuki
 * -----
 * Last Modified: 01/06/2022
 * Modified By: Shun Suzuki (suzuki@hapis.k.u-tokyo.ac.jp)
 * -----
 * Copyright (c) 2022 Shun Suzuki. All rights reserved.
 *
 */

use crate::{
    fpga::{Duty, LegacyDrive, Phase},
    hardware::NUM_TRANS_IN_UNIT,
    Mode, POINT_STM_FIXED_NUM_UNIT,
};

#[derive(Clone, Copy)]
#[repr(C)]
pub struct Body {
    pub data: [u16; NUM_TRANS_IN_UNIT],
}

impl Body {
    pub fn new() -> Self {
        Self {
            data: [0x0000; NUM_TRANS_IN_UNIT],
        }
    }

    pub fn legacy_drives_mut(&mut self) -> &mut [LegacyDrive; NUM_TRANS_IN_UNIT] {
        unsafe { std::mem::transmute(&mut self.data) }
    }

    pub fn duties_mut(&mut self) -> &mut [Duty; NUM_TRANS_IN_UNIT] {
        unsafe { std::mem::transmute(&mut self.data) }
    }

    pub fn phases_mut(&mut self) -> &mut [Phase; NUM_TRANS_IN_UNIT] {
        unsafe { std::mem::transmute(&mut self.data) }
    }

    pub fn point_stm_head(&self) -> &PointSTMBodyHead {
        unsafe { std::mem::transmute(self) }
    }

    pub fn point_stm_head_mut(&mut self) -> &mut PointSTMBodyHead {
        unsafe { std::mem::transmute(self) }
    }

    pub fn point_stm_body(&self) -> &PointSTMBodyBody {
        unsafe { std::mem::transmute(self) }
    }

    pub fn point_stm_body_mut(&mut self) -> &mut PointSTMBodyBody {
        unsafe { std::mem::transmute(self) }
    }

    pub fn gain_stm_head(&self) -> &GainSTMBodyHead {
        unsafe { std::mem::transmute(self) }
    }

    pub fn gain_stm_head_mut(&mut self) -> &mut GainSTMBodyHead {
        unsafe { std::mem::transmute(self) }
    }

    pub fn gain_stm_body(&self) -> &GainSTMBodyBody {
        unsafe { std::mem::transmute(self) }
    }

    pub fn gain_stm_body_mut(&mut self) -> &mut GainSTMBodyBody {
        unsafe { std::mem::transmute(self) }
    }
}

impl Default for Body {
    fn default() -> Self {
        Self::new()
    }
}

#[repr(C)]
pub struct SeqFocus {
    pub(crate) buf: [u16; 4],
}

impl SeqFocus {
    pub fn new(x: f64, y: f64, z: f64, duty_shift: u8) -> Self {
        let x = (x / POINT_STM_FIXED_NUM_UNIT).round() as i32;
        let y = (y / POINT_STM_FIXED_NUM_UNIT).round() as i32;
        let z = (z / POINT_STM_FIXED_NUM_UNIT).round() as i32;
        let d0 = (x & 0xFFFF) as u16;
        let d1 =
            ((y << 2) & 0xFFFC) as u16 | ((x >> 30) & 0x0002) as u16 | ((x >> 16) & 0x0001) as u16;
        let d2 =
            ((z << 4) & 0xFFF0) as u16 | ((y >> 28) & 0x0008) as u16 | ((y >> 14) & 0x0007) as u16;
        let d3 = (((duty_shift as u16) << 6) & 0x3FC0) as u16
            | ((z >> 26) & 0x0020) as u16
            | ((z >> 12) & 0x001F) as u16;
        SeqFocus {
            buf: [d0, d1, d2, d3],
        }
    }
}

#[repr(C)]
pub struct PointSTMBodyHead {
    data: [u16; NUM_TRANS_IN_UNIT],
}

impl PointSTMBodyHead {
    pub fn data(&self) -> &[u16] {
        &self.data
    }

    pub fn set_size(&mut self, size: u16) {
        self.data[0] = size;
    }

    pub fn set_freq_div(&mut self, freq_div: u32) {
        self.data[1] = (freq_div & 0x0000FFFF) as _;
        self.data[2] = ((freq_div >> 16) & 0x0000FFFF) as _;
    }

    pub fn set_sound_speed(&mut self, sound_speed: u32) {
        self.data[3] = (sound_speed & 0x0000FFFF) as _;
        self.data[4] = ((sound_speed >> 16) & 0x0000FFFF) as _;
    }

    pub fn set_points(&mut self, points: &[SeqFocus]) {
        self.data[5..]
            .chunks_mut(4)
            .zip(points.iter())
            .for_each(|(d, s)| d.copy_from_slice(&s.buf));
    }
}

#[repr(C)]
pub struct PointSTMBodyBody {
    data: [u16; NUM_TRANS_IN_UNIT],
}

impl PointSTMBodyBody {
    pub fn data(&self) -> &[u16] {
        &self.data
    }

    pub fn set_size(&mut self, size: u16) {
        self.data[0] = size;
    }

    pub fn set_points(&mut self, points: &[SeqFocus]) {
        self.data[1..]
            .chunks_mut(4)
            .zip(points.iter())
            .for_each(|(d, s)| d.copy_from_slice(&s.buf));
    }
}

#[repr(C)]
pub struct GainSTMBodyHead {
    data: [u16; NUM_TRANS_IN_UNIT],
}

impl GainSTMBodyHead {
    pub fn data(&self) -> &[u16] {
        &self.data
    }

    pub fn set_freq_div(&mut self, freq_div: u32) {
        self.data[0] = (freq_div & 0x0000FFFF) as _;
        self.data[1] = ((freq_div >> 16) & 0x0000FFFF) as _;
    }

    pub fn set_mode(&mut self, mode: Mode) {
        self.data[2] = mode as u16;
    }
}

#[repr(C)]
pub struct GainSTMBodyBody {
    data: [u16; NUM_TRANS_IN_UNIT],
}

pub struct PhaseFull {
    pub(crate) phase_0: u8,
    pub(crate) phase_1: u8,
}

pub struct PhaseHalf {
    phase_01: u8,
    phase_23: u8,
}

impl PhaseHalf {
    pub fn set(&mut self, idx: usize, phase: u8) {
        match idx {
            0 => self.phase_01 = (self.phase_01 & 0xF0) | ((phase >> 4) & 0x0F),
            1 => self.phase_01 = (self.phase_01 & 0x0F) | (phase & 0xF0),
            2 => self.phase_23 = (self.phase_23 & 0xF0) | ((phase >> 4) & 0x0F),
            3 => self.phase_23 = (self.phase_23 & 0x0F) | (phase & 0xF0),
            _ => unreachable!(),
        }
    }
}

impl GainSTMBodyBody {
    pub fn data(&self) -> &[u16] {
        &self.data
    }

    pub fn legacy_drives_mut(&mut self) -> &mut [LegacyDrive; NUM_TRANS_IN_UNIT] {
        unsafe { std::mem::transmute(&mut self.data) }
    }

    pub fn phases_mut(&mut self) -> &mut [Phase; NUM_TRANS_IN_UNIT] {
        unsafe { std::mem::transmute(&mut self.data) }
    }

    pub fn duties_mut(&mut self) -> &mut [Duty; NUM_TRANS_IN_UNIT] {
        unsafe { std::mem::transmute(&mut self.data) }
    }

    pub fn phase_full_mut(&mut self) -> &mut [PhaseFull; NUM_TRANS_IN_UNIT] {
        unsafe { std::mem::transmute(&mut self.data) }
    }

    pub fn phase_half_mut(&mut self) -> &mut [PhaseHalf; NUM_TRANS_IN_UNIT] {
        unsafe { std::mem::transmute(&mut self.data) }
    }
}