@specy/risc-v 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,467 @@
1
+ type JsInstructionToken = {
2
+ sourceLine: number;
3
+ sourceColumn: number;
4
+ originalSourceLine: number;
5
+ value: string;
6
+ type: string;
7
+ };
8
+ declare enum StopReason {
9
+ BREAKPOINT = 0,
10
+ EXCEPTION = 1,
11
+ MAX_STEPS = 2,// includes step mode (where maxSteps is 1)
12
+ NORMAL_TERMINATION = 3,
13
+ CLIFF_TERMINATION = 4,// run off bottom of program
14
+ PAUSE = 5,
15
+ STOP = 6
16
+ }
17
+ declare enum DialogType {
18
+ ERROR_MESSAGE = 0,
19
+ INFORMATION_MESSAGE = 1,
20
+ WARNING_MESSAGE = 2,
21
+ QUESTION_MESSAGE = 3
22
+ }
23
+ declare enum ConfirmResult {
24
+ YES = 0,
25
+ NO = 1,
26
+ CANCEL = 2
27
+ }
28
+ type HandlerMap = {
29
+ openFile: {
30
+ in: [filename: string, flags: number, append: boolean];
31
+ out: number;
32
+ };
33
+ closeFile: {
34
+ in: [fileDescriptor: number];
35
+ out: void;
36
+ };
37
+ writeFile: {
38
+ in: [fileDescriptor: number, buffer: number[]];
39
+ out: void;
40
+ };
41
+ readFile: {
42
+ in: [fileDescriptor: number, destination: number[], length: number];
43
+ out: number;
44
+ };
45
+ confirm: {
46
+ in: [message: string];
47
+ out: ConfirmResult;
48
+ };
49
+ inputDialog: {
50
+ in: [message: string];
51
+ out: string;
52
+ };
53
+ outputDialog: {
54
+ in: [message: string, type: DialogType];
55
+ out: void;
56
+ };
57
+ askDouble: {
58
+ in: [message: string];
59
+ out: number;
60
+ };
61
+ askFloat: {
62
+ in: [message: string];
63
+ out: number;
64
+ };
65
+ askInt: {
66
+ in: [message: string];
67
+ out: number;
68
+ };
69
+ askString: {
70
+ in: [message: string];
71
+ out: string;
72
+ };
73
+ readDouble: {
74
+ in: [];
75
+ out: number;
76
+ };
77
+ readFloat: {
78
+ in: [];
79
+ out: number;
80
+ };
81
+ readInt: {
82
+ in: [];
83
+ out: number;
84
+ };
85
+ readString: {
86
+ in: [];
87
+ out: string;
88
+ };
89
+ readChar: {
90
+ in: [];
91
+ out: string;
92
+ };
93
+ logLine: {
94
+ in: [message: string];
95
+ out: void;
96
+ };
97
+ log: {
98
+ in: [message: string];
99
+ out: void;
100
+ };
101
+ printChar: {
102
+ in: [c: string];
103
+ out: void;
104
+ };
105
+ printDouble: {
106
+ in: [d: number];
107
+ out: void;
108
+ };
109
+ printFloat: {
110
+ in: [f: number];
111
+ out: void;
112
+ };
113
+ printInt: {
114
+ in: [i: number];
115
+ out: void;
116
+ };
117
+ printString: {
118
+ in: [l: string];
119
+ out: void;
120
+ };
121
+ sleep: {
122
+ in: [milliseconds: number];
123
+ out: void;
124
+ };
125
+ stdIn: {
126
+ in: [buffer: number[], length: number];
127
+ out: void;
128
+ };
129
+ stdOut: {
130
+ in: [buffer: number[]];
131
+ out: void;
132
+ };
133
+ };
134
+ type JsInstruction = {
135
+ name: string;
136
+ example: string;
137
+ description: string;
138
+ tokens: JsInstructionToken[];
139
+ };
140
+ type RiscvTokenizedLine = {
141
+ line: string;
142
+ tokens: JsInstructionToken[];
143
+ };
144
+ type RISCVAssembleError = {
145
+ isWarning: boolean;
146
+ message: string;
147
+ macroExpansionHistory: string;
148
+ filename: string;
149
+ lineNumber: number;
150
+ columnNumber: number;
151
+ };
152
+ type RISCVAssembleResult = {
153
+ report: string;
154
+ errors: RISCVAssembleError[];
155
+ hasErrors: boolean;
156
+ };
157
+ declare class RISCV {
158
+ static makeRiscVFromSource: typeof makeRiscVfromSource;
159
+ static initializeRISCV: typeof initializeRISCV;
160
+ static getInstructionSet(): JsInstruction[];
161
+ static setIs64Bit(is64Bit: boolean): void;
162
+ }
163
+ type JsRiscVStackFrame = {
164
+ /**
165
+ * The program counter value at the moment the stack frame was created.
166
+ */
167
+ pc: number;
168
+ /**
169
+ * The address of the target instruction.
170
+ */
171
+ toAddress: number;
172
+ /**
173
+ * The stack pointer value at the moment the stack frame was created.
174
+ */
175
+ sp: number;
176
+ /**
177
+ * The frame pointer value at the moment the stack frame was created.
178
+ */
179
+ fp: number;
180
+ /**
181
+ * The values of all registers at the moment the stack frame was created.
182
+ */
183
+ registers: number[];
184
+ };
185
+ /**
186
+ * Represents a statement in the assembled program.
187
+ */
188
+ interface JsProgramStatement {
189
+ /**
190
+ * The line number in the original source code.
191
+ */
192
+ readonly sourceLine: number;
193
+ /**
194
+ * The memory address of the instruction.
195
+ */
196
+ readonly address: number;
197
+ /**
198
+ * The binary representation of the instruction.
199
+ */
200
+ readonly binaryStatement: number;
201
+ /**
202
+ * The original source code line.
203
+ */
204
+ readonly source: string;
205
+ /**
206
+ * The machine code representation of the instruction.
207
+ */
208
+ readonly machineStatement: string;
209
+ /**
210
+ * The assembly representation of the instruction.
211
+ */
212
+ readonly assemblyStatement: string;
213
+ }
214
+ /**
215
+ * Enum representing the types of "undo" actions.
216
+ */
217
+ declare enum BackStepAction {
218
+ MEMORY_RESTORE_RAW_WORD = 0,
219
+ MEMORY_RESTORE_WORD = 1,
220
+ MEMORY_RESTORE_HALF = 2,
221
+ MEMORY_RESTORE_BYTE = 3,
222
+ REGISTER_RESTORE = 4,
223
+ PC_RESTORE = 5,
224
+ COPROC0_REGISTER_RESTORE = 6,
225
+ COPROC1_REGISTER_RESTORE = 7,
226
+ COPROC1_CONDITION_CLEAR = 8,
227
+ COPROC1_CONDITION_SET = 9,
228
+ DO_NOTHING = 10
229
+ }
230
+ /**
231
+ * Represents a back step in the simulation undo stack.
232
+ */
233
+ interface JsBackStep {
234
+ /**
235
+ * The action performed (e.g., register write, memory write).
236
+ */
237
+ readonly action: BackStepAction;
238
+ /**
239
+ * Information about the action
240
+ */
241
+ readonly param1: number;
242
+ /**
243
+ * Information about the action
244
+ */
245
+ readonly param2: number;
246
+ /**
247
+ * The program counter value before the action.
248
+ */
249
+ readonly pc: number;
250
+ }
251
+ declare enum RISCVRegisters {
252
+ zero = 0,
253
+ ra = 1,
254
+ sp = 2,
255
+ gp = 3,
256
+ tp = 4,
257
+ t0 = 5,
258
+ t1 = 6,
259
+ t2 = 7,
260
+ s0 = 8,
261
+ s1 = 9,
262
+ a0 = 10,
263
+ a1 = 11,
264
+ a2 = 12,
265
+ a3 = 13,
266
+ a4 = 14,
267
+ a5 = 15,
268
+ a6 = 16,
269
+ a7 = 17,
270
+ s2 = 18,
271
+ s3 = 19,
272
+ s4 = 20,
273
+ s5 = 21,
274
+ s6 = 22,
275
+ s7 = 23,
276
+ s8 = 24,
277
+ s9 = 25,
278
+ s10 = 26,
279
+ s11 = 27,
280
+ t3 = 28,
281
+ t4 = 29,
282
+ t5 = 30,
283
+ t6 = 31
284
+ }
285
+ type RegisterName = keyof typeof RISCVRegisters;
286
+ declare const RISCV_REGISTERS: RegisterName[];
287
+ type HandlerName = keyof HandlerMap;
288
+ type HandlerMapFns = {
289
+ [K in HandlerName]: (...args: HandlerMap[K]['in']) => HandlerMap[K]['out'];
290
+ };
291
+ declare function registerHandlers(riscv: JsRiscV, handlers: HandlerMapFns): void;
292
+ declare function unimplementedHandler(name: HandlerName): () => never;
293
+ /**
294
+ * Interface for interacting with a RISCV simulator.
295
+ */
296
+ interface JsRiscV {
297
+ /**
298
+ * Assembles the program.
299
+ */
300
+ assemble(): RISCVAssembleResult;
301
+ /**
302
+ * Initializes the simulator.
303
+ * @param startAtMain If true, starts execution at the 'main' label. Otherwise, starts at the first instruction.
304
+ */
305
+ initialize(startAtMain: boolean): void;
306
+ /**
307
+ * Gets the current stop reason.
308
+ */
309
+ getStopReason(): StopReason;
310
+ /**
311
+ * Executes a single instruction.
312
+ * @returns True if the execution is complete, false otherwise.
313
+ */
314
+ step(): StopReason;
315
+ /**
316
+ * Gets the 8 condition flags.
317
+ */
318
+ getConditionFlags(): number[];
319
+ /**
320
+ * Sets the size of the undo stack, must be called before assembling the program.
321
+ * @param size
322
+ */
323
+ setUndoSize(size: number): void;
324
+ /**
325
+ * Undoes the last instruction executed.
326
+ */
327
+ undo(): void;
328
+ /**
329
+ * Gets the statement at the given address.
330
+ * @param address
331
+ */
332
+ getStatementAtAddress(address: number): JsProgramStatement;
333
+ /**
334
+ * Gets the statement at the given source line.
335
+ * @param line
336
+ */
337
+ getStatementAtSourceLine(line: number): JsProgramStatement;
338
+ getTokenizedLines(): RiscvTokenizedLine[];
339
+ /**
340
+ * Checks if the simulation can be undone.
341
+ * @returns True if the simulation can be undone, false otherwise.
342
+ * */
343
+ canUndo: boolean;
344
+ /**
345
+ * Gets the call stack.
346
+ * @returns An array of memory addresses representing the call stack.
347
+ */
348
+ getCallStack(): JsRiscVStackFrame[];
349
+ /**
350
+ * Gets the compiled statements.
351
+ * @returns An array of `JsProgramStatement` objects representing the compiled program.
352
+ */
353
+ getCompiledStatements(): JsProgramStatement[];
354
+ getParsedStatements(): JsInstructionToken[];
355
+ /**
356
+ * Gets the label at the given address.
357
+ * @param address The memory address.
358
+ * @returns The label at the given address, or null if no label is found.
359
+ */
360
+ getLabelAtAddress(address: number): string | null;
361
+ /**
362
+ * Sets whether the undo feature is enabled.
363
+ * @param enabled True to enable the undo feature, false to disable it.
364
+ */
365
+ setUndoEnabled(enabled: boolean): void;
366
+ /**
367
+ * Simulates until the stop condition is met. it might be a breakpoint, exception, etc...
368
+ */
369
+ simulate(): StopReason;
370
+ /**
371
+ * Simulates the program for a limited number of instructions.
372
+ * @param limit The maximum number of instructions to execute.
373
+ * @returns True if the execution is complete, false otherwise.
374
+ */
375
+ simulateWithLimit(limit: number): StopReason;
376
+ /**
377
+ * Simulates the program until a breakpoint is reached.
378
+ * @param breakpoints An array of memory addresses where the simulation should pause.
379
+ * @returns True if the execution is complete, false otherwise.
380
+ */
381
+ simulateWithBreakpoints(breakpoints: number[]): StopReason;
382
+ /**
383
+ * Simulates the program with both breakpoints and a limit.
384
+ * @param breakpoints An array of memory addresses where the simulation should pause.
385
+ * @param limit The maximum number of instructions to execute.
386
+ * @returns True if the execution is complete, false otherwise.
387
+ */
388
+ simulateWithBreakpointsAndLimit(breakpoints: number[], limit: number): StopReason;
389
+ /**
390
+ * Gets the value of a register.
391
+ * @param register The name of the register.
392
+ * @returns The value of the register.
393
+ */
394
+ getRegisterValue(register: RegisterName): number;
395
+ /**
396
+ * Registers a handler function for a specific event or condition.
397
+ * @param name The name of the event or condition.
398
+ * @param handler The handler function to be called when the event occurs. The function signature depends on the event name.
399
+ */
400
+ registerHandler<T extends HandlerName>(name: T, handler: (...args: HandlerMap[T]['in']) => HandlerMap[T]['out']): void;
401
+ /**
402
+ * Gets the current value of the stack pointer.
403
+ * @returns The value of the stack pointer.
404
+ */
405
+ stackPointer: number;
406
+ /**
407
+ * Gets the current value of the program counter.
408
+ * @returns The value of the program counter.
409
+ */
410
+ programCounter: number;
411
+ /**
412
+ * Gets the values of all registers.
413
+ * @returns An array containing the register values. The order of the values is implementation defined.
414
+ */
415
+ getRegistersValues(): number[];
416
+ /**
417
+ * Gets the undo stack.
418
+ * @returns An array of `JsBackStep` objects representing the history of the simulation.
419
+ */
420
+ getUndoStack(): JsBackStep[];
421
+ /**
422
+ * Reads a sequence of bytes from memory.
423
+ * @param address The starting memory address.
424
+ * @param length The number of bytes to read.
425
+ * @returns An array of bytes read from memory.
426
+ */
427
+ readMemoryBytes(address: number, length: number): number[];
428
+ /**
429
+ * Writes a sequence of bytes to memory.
430
+ * @param address The starting memory address.
431
+ * @param bytes An array of bytes to write to memory.
432
+ */
433
+ setMemoryBytes(address: number, bytes: number[]): void;
434
+ /**
435
+ * Gets the index of the current statement in the assembled program.
436
+ * @returns The index of the current statement.
437
+ */
438
+ getCurrentStatementIndex(): number;
439
+ /**
440
+ * Gets the next statement to be executed.
441
+ * @returns The next `JsProgramStatement`.
442
+ */
443
+ getNextStatement(): JsProgramStatement;
444
+ /**
445
+ * Sets the value of a register.
446
+ * @param register The name of the register.
447
+ * @param value The value to set the register to.
448
+ */
449
+ setRegisterValue(register: RegisterName, value: number): void;
450
+ /**
451
+ * Checks if the simulation has terminated.
452
+ * @returns True if the simulation has terminated, false otherwise.
453
+ */
454
+ terminated: boolean;
455
+ }
456
+ /**
457
+ * Creates a new RISCV simulator from the given source code.
458
+ * @param source The source code to assemble.
459
+ * @returns A new `JsRiscV` object.
460
+ */
461
+ declare function makeRiscVfromSource(source: string): JsRiscV;
462
+ /**
463
+ * Initializes the RISCV simulator.
464
+ */
465
+ declare function initializeRISCV(): void;
466
+
467
+ export { BackStepAction, ConfirmResult, DialogType, type HandlerMap, type HandlerMapFns, type JsBackStep, type JsInstruction, type JsInstructionToken, type JsProgramStatement, type JsRiscV, type JsRiscVStackFrame, RISCV, type RISCVAssembleError, type RISCVAssembleResult, RISCVRegisters, RISCV_REGISTERS, type RegisterName, type RiscvTokenizedLine, StopReason, registerHandlers, unimplementedHandler };