summaryrefslogtreecommitdiff
path: root/libs/JSystem/src/JAudio2/JASSeqReader.cpp
blob: 459c2edeee0b6b422bf2c3ba1cec37a1157bed62 (plain)
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
/**
 * JASSeqReader.cpp
 * 
 */

#include "JSystem/JSystem.h" // IWYU pragma: keep

#include "JSystem/JAudio2/JASSeqReader.h"

void JASSeqReader::init() {
    mBase = 0;
    mCurPos = 0;
    mCurStackDepth = 0;

    for (int i = 0; i < JAS_SEQ_STACK_SIZE; i++) {
        mReturnAddr[i] = NULL;
        mLoopCount[i] = 0;
    }
}

void JASSeqReader::init(void* base) {
    mBase = (u8*)base;
    mCurPos = mBase;
    mCurStackDepth = 0;

    for (int i = 0; i < JAS_SEQ_STACK_SIZE; i++) {
        mReturnAddr[i] = NULL;
        mLoopCount[i] = 0;
    }
    
}

bool JASSeqReader::call(u32 param_0) {
    if (mCurStackDepth >= JAS_SEQ_STACK_SIZE) {
        JUT_WARN(42, "%s", "Cannot exec call command");
        return false;
    }

    mReturnAddr[mCurStackDepth++] = (u16*)mCurPos;
    mCurPos = mBase + param_0;

    return true;
}

bool JASSeqReader::loopStart(u32 param_0) {
    if (JAS_SEQ_STACK_SIZE <= mCurStackDepth) {
        JUT_WARN(53, "%s", "Cannot exec loopStart command");
        return false;
    }

    mReturnAddr[mCurStackDepth] = (u16*)mCurPos;
    mLoopCount[mCurStackDepth++] = param_0;

    return true;
}


bool JASSeqReader::loopEnd() {
    if (mCurStackDepth == 0) {
        JUT_WARN(65, "%s", "cannot loopE for call-stack is NULL");
        return false;
    }

    u16 tmp = mLoopCount[mCurStackDepth - 1];

    if (tmp != 0) {
        tmp--;
    }

    if (!tmp) {
        mCurStackDepth--;
        return true;
    }

    mLoopCount[mCurStackDepth - 1] = tmp;
    mCurPos = (u8*)mReturnAddr[mCurStackDepth - 1];
    return true;
}

bool JASSeqReader::ret() {
    if (mCurStackDepth == 0) {
        return false;
    }

    mCurPos = (u8*)mReturnAddr[--mCurStackDepth];

    return true;
}

int JASSeqReader::readMidiValue() {
    int byte = readByte();

    if (!(byte & 0x80)) {
        return byte;
    }

    byte &= 0x7f;

    int i = 0;

    while (true) {
        if (2 < i) {
            JUT_WARN(100, "%s", "readMidiValue: Too large value");
            return 0;
        }

        u8 newByte = readByte();
        
        byte = byte << 7;
        byte |= newByte & 0x7f;

        if (!(newByte & 0x80)) {
            break;
        }

        i++;
    }

    return byte;
}