aboutsummaryrefslogtreecommitdiffgithub
path: root/packages/novice-web/novice-web/components/GuiDebugger.tsx
blob: 3a540bda8cb59143c5583e0394a38dba9012575e (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
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
import { BaseSymbols, fmtBinOrHex, fmtHex, FullMachineState, getIsa, Isa, range,
         Symbols } from 'novice';
import * as React from 'react';
import { VariableSizeGrid as Grid } from 'react-window';
import { AssemblerFrontendMessage, AssemblerWorkerMessage } from '../workers/assembler';
import { DebuggerFrontendMessage, DebuggerWorkerMessage } from '../workers/debugger';
import { AssembleForm } from './AssembleForm';

export interface GuiDebuggerProps {
    debuggerWorkerBundleUrl: string;
    assemblerWorkerBundleUrl: string;
    isaName: string;
    initialAssemblyCode: string;
}

export interface GuiDebuggerState {
    state: FullMachineState;
}

export class GuiDebugger extends React.Component<GuiDebuggerProps,
                                                 GuiDebuggerState> {
    private isa: Isa;
    private symbols: Symbols;
    private debuggerWorker: Worker;
    private assemblerWorker: Worker;

    constructor(props: GuiDebuggerProps) {
        super(props);

        this.isa = getIsa(this.props.isaName);
        this.symbols = new BaseSymbols();
        this.state = {
            state: this.isa.initMachineState(),
        };

        this.onError = this.onError.bind(this);
        this.onDebuggerMessage = this.onDebuggerMessage.bind(this);
        this.onAssemblerMessage = this.onAssemblerMessage.bind(this);
        this.handleAssembleRequest = this.handleAssembleRequest.bind(this);
        this.handleStepRequest = this.handleStepRequest.bind(this);
        this.handleUnstepRequest = this.handleUnstepRequest.bind(this);
        this.handleContinueRequest = this.handleContinueRequest.bind(this);

        this.debuggerWorker = this.loadWorkerBundle(this.props.debuggerWorkerBundleUrl,
                                                    this.onDebuggerMessage);
        this.assemblerWorker = this.loadWorkerBundle(this.props.assemblerWorkerBundleUrl,
                                                     this.onAssemblerMessage);

        this.postDebuggerMessage({ kind: 'reset', isa: this.props.isaName });
    }

    public componentDidUpdate(prevProps: GuiDebuggerProps) {
        // If ISA changed, reset the worker
        if (prevProps.isaName !== this.props.isaName) {
            this.isa = getIsa(this.props.isaName);
            this.setState({ state: this.isa.initMachineState() });
            this.postDebuggerMessage({ kind: 'reset', isa: this.props.isaName });
        }
    }

    public render() {
        // Be a little dishonest: to avoid confusing users, get 'stuck' on halts
        const pc = this.state.state.halted ? this.state.state.pc - this.isa.spec.pc.increment
                                           : this.state.state.pc;

        const registers = this.isa.spec.regs.map(reg => {
            let name = '';
            let values;

            if (reg.kind === 'reg-range') {
                name = 'range-' + reg.prefix;
                values = range(reg.count).map(i => (<div className='reg' key={name + i}>{
                    reg.prefix + i + ': ' + fmtBinOrHex(this.state.state.regs.range[reg.prefix][i], reg.bits)
                }</div>));
            } else if (reg.kind === 'reg') {
                name = 'solo-' + reg.name;
                values = (<div className='reg' key={name}>
                    {reg.name + ': '  + fmtBinOrHex(this.state.state.regs.solo[reg.name], reg.bits)}
                </div>);
            } else {
                const _: never = reg;
            }

            return (<div className='reg-family' key={'family-' + name}>{values}</div>);
        });

        const rowHeight = 20;
        const cols = [20, 80, 80, 80, 200];
        const colVal: ((addr: number) => string)[] = [
            addr => (pc === addr) ? '►' : '',
            addr => this.fmtAddr(addr),
            addr => this.fmtWord(this.isa.stateLoad(this.state.state, addr)),
            addr => this.isa.stateLoad(this.state.state, addr).toString(10),
            addr => this.isa.disassemble(addr, this.isa.stateLoad(this.state.state, addr),
                                         this.symbols, true) || '',
        ];

        const cell = (props: { columnIndex: number,
                               rowIndex: number,
                               style: React.CSSProperties }) => (
            <div style={props.style}>
                {colVal[props.columnIndex](props.rowIndex)}
            </div>
        );

        return (
            <div className='gui-wrapper'>
                <div className='state-view'>
                    <div className='register-view'>
                        {registers}
                    </div>
                    <Grid columnCount={cols.length}
                          columnWidth={i => cols[i]}
                          rowCount={Math.pow(2, this.isa.spec.mem.space)}
                          rowHeight={i => rowHeight}
                          width={cols.reduce((acc, cur) => acc + cur) + 32}
                          height={600}
                          initialScrollTop={this.state.state.pc * rowHeight}>{cell}</Grid>
                </div>
                <AssembleForm initialAssemblyCode={this.props.initialAssemblyCode}
                              handleAssembleRequest={this.handleAssembleRequest}
                              handleStepRequest={this.handleStepRequest}
                              handleUnstepRequest={this.handleUnstepRequest}
                              handleContinueRequest={this.handleContinueRequest} />
            </div>
        );
    }

    private onError(err: ErrorEvent) {
        console.error(err);
    }

    private onDebuggerMessage(msg: DebuggerWorkerMessage) {
        switch (msg.kind) {
            case 'updates':
                const [state, _] = this.isa.stateApplyUpdates(
                    this.state.state, msg.updates);
                this.setState({ state });
                break;

            case 'putc':
                // TODO: actually display somehow
                console.log(String.fromCharCode(msg.c));
                break;

            default:
                const __: never = msg;
        }
    }

    private onAssemblerMessage(msg: AssemblerWorkerMessage) {
        switch (msg.kind) {
            case 'assembly-finished':
                const sections = msg.sections;
                this.postDebuggerMessage({ kind: 'load-sections', sections });
                break;

            case 'assembly-error':
                const errorMessage = msg.errorMessage;
                console.error('assembly error in frontend', errorMessage);
                break;

            default:
                const __: never = msg;
        }
    }

    private loadWorkerBundle<WorkerMessageType>(workerBundleUrl: string,
                                                onMessage: (msg: WorkerMessageType) => void): Worker {
        const worker = new Worker(workerBundleUrl);
        worker.onerror = this.onError;
        worker.onmessage = (e: MessageEvent) => {
            const msg: WorkerMessageType = e.data;
            onMessage(msg);
        };
        return worker;
    }

    private postDebuggerMessage(msg: DebuggerFrontendMessage): void {
        this.debuggerWorker.postMessage(msg);
    }

    private postAssemblerMessage(msg: AssemblerFrontendMessage): void {
        this.assemblerWorker.postMessage(msg);
    }

    private handleAssembleRequest(assemblyCode: string): void {
        // TODO: it's a hack to assume isa name == assembler config name
        this.postAssemblerMessage({ kind: 'assemble',
                                    configName: this.props.isaName,
                                    assemblyCode });
    }

    private handleStepRequest(): void {
        this.postDebuggerMessage({ kind: 'step' });
    }

    private handleUnstepRequest(): void {
        this.postDebuggerMessage({ kind: 'unstep' });
    }

    private handleContinueRequest(): void {
        this.postDebuggerMessage({ kind: 'run' });
    }

    private fmtAddr(addr: number): string {
        return fmtHex(addr, this.isa.spec.mem.space);
    }

    private fmtWord(word: number): string {
        return fmtHex(word, this.isa.spec.mem.word);
    }
}