frame-isa 0.1.0

SAM Instruction Set Architecture - 6-byte opcode definitions for AI systems
Documentation
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
//! Modifier flags for the SAM ISA
//!
//! Modifiers control the style and attributes of opcode output. Each modifier is
//! a 2-byte bit-packed value with multiple fields.

use serde::{Deserialize, Serialize};
use std::fmt;

/// Modifier flags (2 bytes)
///
/// Controls styling and attributes of opcode output. Bit layout:
///
/// ```text
/// Bit:  15  14  13  12  11  10   9   8   7   6   5   4   3   2   1   0
///       [--VOICE--] [--TONE--] [-WARM-] [--FORMAT--] [ACCURACY] [URGENCY]
/// ```
///
/// - **Voice** (bits 15-14): Speaking style - Neutral, Formal, Casual, Technical
/// - **Tone** (bits 13-12): Emotional tone - Neutral, Positive, Empathetic, Cautious
/// - **Warmth** (bits 11-10): Interpersonal warmth - Cold, Neutral, Warm, VeryWarm
/// - **Format** (bits 9-8): Output format - Prose, Bulleted, Numbered, Structured
/// - **Accuracy** (bits 7-6): Confidence level - Low, Medium, High, Verified
/// - **Urgency** (bits 5-4): Priority level - Low, Normal, High, Critical
/// - **Reserved** (bits 3-0): For future use
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[repr(transparent)]
pub struct Modifier(pub u16);

impl Modifier {
    // ========== Voice Styles (bits 15-14) ==========
    pub const VOICE_NEUTRAL: Self = Self(0x0000);
    pub const VOICE_FORMAL: Self = Self(0x4000);
    pub const VOICE_CASUAL: Self = Self(0x8000);
    pub const VOICE_TECHNICAL: Self = Self(0xC000);

    // ========== Tone (bits 13-12) ==========
    pub const TONE_NEUTRAL: Self = Self(0x0000);
    pub const TONE_POSITIVE: Self = Self(0x1000);
    pub const TONE_EMPATHETIC: Self = Self(0x2000);
    pub const TONE_CAUTIOUS: Self = Self(0x3000);

    // ========== Warmth (bits 11-10) ==========
    pub const WARMTH_COLD: Self = Self(0x0000);
    pub const WARMTH_NEUTRAL: Self = Self(0x0400);
    pub const WARMTH_WARM: Self = Self(0x0800);
    pub const WARMTH_VERY_WARM: Self = Self(0x0C00);

    // ========== Format (bits 9-8) ==========
    pub const FORMAT_PROSE: Self = Self(0x0000);
    pub const FORMAT_BULLETED: Self = Self(0x0100);
    pub const FORMAT_NUMBERED: Self = Self(0x0200);
    pub const FORMAT_STRUCTURED: Self = Self(0x0300);

    // ========== Accuracy (bits 7-6) ==========
    pub const ACCURACY_LOW: Self = Self(0x0000);
    pub const ACCURACY_MEDIUM: Self = Self(0x0040);
    pub const ACCURACY_HIGH: Self = Self(0x0080);
    pub const ACCURACY_VERIFIED: Self = Self(0x00C0);

    // ========== Urgency (bits 5-4) ==========
    pub const URGENCY_LOW: Self = Self(0x0000);
    pub const URGENCY_NORMAL: Self = Self(0x0010);
    pub const URGENCY_HIGH: Self = Self(0x0020);
    pub const URGENCY_CRITICAL: Self = Self(0x0030);

    // ========== Bit Masks ==========
    const VOICE_MASK: u16 = 0xC000;
    const TONE_MASK: u16 = 0x3000;
    const WARMTH_MASK: u16 = 0x0C00;
    const FORMAT_MASK: u16 = 0x0300;
    const ACCURACY_MASK: u16 = 0x00C0;
    const URGENCY_MASK: u16 = 0x0030;

    /// Create from raw u16 value
    #[inline]
    pub const fn from_u16(value: u16) -> Self {
        Self(value)
    }

    /// Get raw u16 value
    #[inline]
    pub const fn as_u16(&self) -> u16 {
        self.0
    }

    /// Get voice style
    #[inline]
    pub const fn voice(&self) -> Voice {
        match self.0 & Self::VOICE_MASK {
            0x0000 => Voice::Neutral,
            0x4000 => Voice::Formal,
            0x8000 => Voice::Casual,
            _ => Voice::Technical,
        }
    }

    /// Get tone
    #[inline]
    pub const fn tone(&self) -> Tone {
        match self.0 & Self::TONE_MASK {
            0x0000 => Tone::Neutral,
            0x1000 => Tone::Positive,
            0x2000 => Tone::Empathetic,
            _ => Tone::Cautious,
        }
    }

    /// Get warmth level
    #[inline]
    pub const fn warmth(&self) -> Warmth {
        match self.0 & Self::WARMTH_MASK {
            0x0000 => Warmth::Cold,
            0x0400 => Warmth::Neutral,
            0x0800 => Warmth::Warm,
            _ => Warmth::VeryWarm,
        }
    }

    /// Get output format
    #[inline]
    pub const fn format(&self) -> Format {
        match self.0 & Self::FORMAT_MASK {
            0x0000 => Format::Prose,
            0x0100 => Format::Bulleted,
            0x0200 => Format::Numbered,
            _ => Format::Structured,
        }
    }

    /// Get accuracy level
    #[inline]
    pub const fn accuracy(&self) -> Accuracy {
        match self.0 & Self::ACCURACY_MASK {
            0x0000 => Accuracy::Low,
            0x0040 => Accuracy::Medium,
            0x0080 => Accuracy::High,
            _ => Accuracy::Verified,
        }
    }

    /// Get urgency level
    #[inline]
    pub const fn urgency(&self) -> Urgency {
        match self.0 & Self::URGENCY_MASK {
            0x0000 => Urgency::Low,
            0x0010 => Urgency::Normal,
            0x0020 => Urgency::High,
            _ => Urgency::Critical,
        }
    }

    /// Set voice style
    #[inline]
    pub const fn with_voice(self, voice: Voice) -> Self {
        let voice_bits = match voice {
            Voice::Neutral => 0x0000,
            Voice::Formal => 0x4000,
            Voice::Casual => 0x8000,
            Voice::Technical => 0xC000,
        };
        Self((self.0 & !Self::VOICE_MASK) | voice_bits)
    }

    /// Set tone
    #[inline]
    pub const fn with_tone(self, tone: Tone) -> Self {
        let tone_bits = match tone {
            Tone::Neutral => 0x0000,
            Tone::Positive => 0x1000,
            Tone::Empathetic => 0x2000,
            Tone::Cautious => 0x3000,
        };
        Self((self.0 & !Self::TONE_MASK) | tone_bits)
    }

    /// Set warmth level
    #[inline]
    pub const fn with_warmth(self, warmth: Warmth) -> Self {
        let warmth_bits = match warmth {
            Warmth::Cold => 0x0000,
            Warmth::Neutral => 0x0400,
            Warmth::Warm => 0x0800,
            Warmth::VeryWarm => 0x0C00,
        };
        Self((self.0 & !Self::WARMTH_MASK) | warmth_bits)
    }

    /// Set output format
    #[inline]
    pub const fn with_format(self, format: Format) -> Self {
        let format_bits = match format {
            Format::Prose => 0x0000,
            Format::Bulleted => 0x0100,
            Format::Numbered => 0x0200,
            Format::Structured => 0x0300,
        };
        Self((self.0 & !Self::FORMAT_MASK) | format_bits)
    }

    /// Set accuracy level
    #[inline]
    pub const fn with_accuracy(self, accuracy: Accuracy) -> Self {
        let accuracy_bits = match accuracy {
            Accuracy::Low => 0x0000,
            Accuracy::Medium => 0x0040,
            Accuracy::High => 0x0080,
            Accuracy::Verified => 0x00C0,
        };
        Self((self.0 & !Self::ACCURACY_MASK) | accuracy_bits)
    }

    /// Set urgency level
    #[inline]
    pub const fn with_urgency(self, urgency: Urgency) -> Self {
        let urgency_bits = match urgency {
            Urgency::Low => 0x0000,
            Urgency::Normal => 0x0010,
            Urgency::High => 0x0020,
            Urgency::Critical => 0x0030,
        };
        Self((self.0 & !Self::URGENCY_MASK) | urgency_bits)
    }

    /// Create a crisis-appropriate modifier (empathetic, warm, high urgency)
    pub const fn crisis() -> Self {
        Self(0x0000)
            .with_tone(Tone::Empathetic)
            .with_warmth(Warmth::VeryWarm)
            .with_urgency(Urgency::High)
            .with_accuracy(Accuracy::High)
    }

    /// Create a professional modifier (formal, neutral warmth)
    pub const fn professional() -> Self {
        Self(0x0000)
            .with_voice(Voice::Formal)
            .with_warmth(Warmth::Neutral)
            .with_accuracy(Accuracy::High)
            .with_urgency(Urgency::Normal)
    }

    /// Create a friendly modifier (casual, warm)
    pub const fn friendly() -> Self {
        Self(0x0000)
            .with_voice(Voice::Casual)
            .with_tone(Tone::Positive)
            .with_warmth(Warmth::Warm)
            .with_urgency(Urgency::Normal)
    }
}

impl Default for Modifier {
    fn default() -> Self {
        // Neutral voice, tone, warmth; prose format; medium accuracy; normal urgency
        Self(0x0400 | 0x0040 | 0x0010) // WARMTH_NEUTRAL | ACCURACY_MEDIUM | URGENCY_NORMAL
    }
}

impl fmt::Display for Modifier {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "MOD(0x{:04X}: {:?}/{:?}/{:?}/{:?})",
            self.0,
            self.voice(),
            self.tone(),
            self.warmth(),
            self.format()
        )
    }
}

impl From<u16> for Modifier {
    fn from(value: u16) -> Self {
        Self(value)
    }
}

impl From<Modifier> for u16 {
    fn from(modifier: Modifier) -> Self {
        modifier.0
    }
}

// ========== Field Enums ==========

/// Voice style for output
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Voice {
    /// Default neutral voice
    Neutral,
    /// Formal/professional voice
    Formal,
    /// Casual/conversational voice
    Casual,
    /// Technical/precise voice
    Technical,
}

/// Emotional tone
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Tone {
    /// Neutral tone
    Neutral,
    /// Positive/upbeat tone
    Positive,
    /// Empathetic/understanding tone
    Empathetic,
    /// Cautious/careful tone
    Cautious,
}

/// Interpersonal warmth level
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Warmth {
    /// Cold/distant
    Cold,
    /// Neutral warmth
    Neutral,
    /// Warm/friendly
    Warm,
    /// Very warm/caring
    VeryWarm,
}

/// Output format
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Format {
    /// Prose/paragraph format
    Prose,
    /// Bulleted list
    Bulleted,
    /// Numbered list
    Numbered,
    /// Structured/formatted output
    Structured,
}

/// Confidence/accuracy level
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Accuracy {
    /// Low confidence
    Low,
    /// Medium confidence
    Medium,
    /// High confidence
    High,
    /// Verified/certain
    Verified,
}

/// Urgency/priority level
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Urgency {
    /// Low priority
    Low,
    /// Normal priority
    Normal,
    /// High priority
    High,
    /// Critical priority
    Critical,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_default_modifier() {
        let m = Modifier::default();
        assert_eq!(m.voice(), Voice::Neutral);
        assert_eq!(m.tone(), Tone::Neutral);
        assert_eq!(m.warmth(), Warmth::Neutral);
        assert_eq!(m.format(), Format::Prose);
        assert_eq!(m.accuracy(), Accuracy::Medium);
        assert_eq!(m.urgency(), Urgency::Normal);
    }

    #[test]
    fn test_field_extraction() {
        // Casual(0x8000) + Empathetic(0x2000) + Neutral warmth(0x0400) + Medium accuracy(0x0040)
        let m = Modifier::from_u16(0xA440);
        assert_eq!(m.voice(), Voice::Casual);
        assert_eq!(m.tone(), Tone::Empathetic);
        assert_eq!(m.warmth(), Warmth::Neutral);
        assert_eq!(m.accuracy(), Accuracy::Medium);
    }

    #[test]
    fn test_field_setting() {
        let m = Modifier::default()
            .with_voice(Voice::Formal)
            .with_tone(Tone::Empathetic)
            .with_warmth(Warmth::VeryWarm)
            .with_format(Format::Bulleted);

        assert_eq!(m.voice(), Voice::Formal);
        assert_eq!(m.tone(), Tone::Empathetic);
        assert_eq!(m.warmth(), Warmth::VeryWarm);
        assert_eq!(m.format(), Format::Bulleted);
    }

    #[test]
    fn test_crisis_modifier() {
        let m = Modifier::crisis();
        assert_eq!(m.tone(), Tone::Empathetic);
        assert_eq!(m.warmth(), Warmth::VeryWarm);
        assert_eq!(m.urgency(), Urgency::High);
        assert_eq!(m.accuracy(), Accuracy::High);
    }

    #[test]
    fn test_preset_modifiers() {
        let pro = Modifier::professional();
        assert_eq!(pro.voice(), Voice::Formal);

        let friendly = Modifier::friendly();
        assert_eq!(friendly.voice(), Voice::Casual);
        assert_eq!(friendly.warmth(), Warmth::Warm);
    }

    #[test]
    fn test_serialization() {
        let modifier = Modifier::crisis();
        let json = serde_json::to_string(&modifier).unwrap();
        let deserialized: Modifier = serde_json::from_str(&json).unwrap();
        assert_eq!(modifier, deserialized);
    }
}