Skip to content

Commit

Permalink
Merge pull request #20 from lastmjs/initial-structure
Browse files Browse the repository at this point in the history
load hex into memory
  • Loading branch information
electrovir committed Apr 24, 2018
2 parents cb280d8 + eb52feb commit 83ba3d2
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
10 changes: 8 additions & 2 deletions elements/metal-micro-simulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ class MetalMicroSimulator extends HTMLElement {
});
}

loadClick(e: any) {
Store.dispatch({
type: 'LOAD_HEX_CODE_INTO_MEMORY'
});
}

render() {
const state: State = Store.getState();

Expand All @@ -70,7 +76,7 @@ class MetalMicroSimulator extends HTMLElement {
<br>
<div>
Hex code to execute: 0x<input type="text" class="hexCodeInput" oninput="${(e: Event) => this.hexCodeInputChanged(e)}" value="${state.hexCode}">
Source code: 0x<input type="text" class="hexCodeInput" oninput="${(e: Event) => this.hexCodeInputChanged(e)}" value="${state.hexCode}"> <button onclick="${(e: Event) => this.loadClick(e)}">Load</button>
</div>
<div>
Expand Down Expand Up @@ -114,7 +120,7 @@ class MetalMicroSimulator extends HTMLElement {
${new Array(state.numberOfMemoryLocations).fill(0).map((x, index) => {
return html`
<div>
${index}: <input id="memoryLocation${index}" type="text">
${index}: <input id="memoryLocation${index}" type="text" value="${state.memory[index]}">
</div>
`;
})}
Expand Down
23 changes: 22 additions & 1 deletion redux/redux-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ const InitialState: State = {
instructionCycle: 'OPCODE',
registers: [],
currentResult: '',
numberOfCycles: 0
numberOfCycles: 0,
memory: []
};

const RootReducer = (state=InitialState, action: Action) => {
Expand All @@ -39,6 +40,26 @@ const RootReducer = (state=InitialState, action: Action) => {
hexCode: action.hexCode
};
}
case 'LOAD_HEX_CODE_INTO_MEMORY':{
return {
...state,
memory: state.hexCode.split('').reduce((result, character, index, array) => {
if (index >= array.length / 2) {
return result;
}
else {
return {
...result,
finalArray: [...result.finalArray, `${array[result.index]}${array[result.index + 1]}`],
index: result.index + 2
};
}
}, {
finalArray: [],
index: 0
}).finalArray
};
}
case 'STEP': {
switch (state.instructionCycle) {
case 'OPCODE': {
Expand Down
1 change: 1 addition & 0 deletions wasm-metal.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface State {
registers: string[];
currentResult: '';
numberOfCycles: number;
memory: string[];
}

export interface Action {
Expand Down

0 comments on commit 83ba3d2

Please sign in to comment.