Skip to content

Commit

Permalink
Merge pull request #45 from rpendleton/bugfix/FixInputTraps
Browse files Browse the repository at this point in the history
Fix a few issues with input traps
  • Loading branch information
justinmeiners authored Jan 2, 2022
2 parents b5a47f7 + 588458c commit 90ba00c
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
*.out
*.obj
*.exe
*.dSYM

lc3
lc3-alt
10 changes: 8 additions & 2 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,9 @@ <h3> Procedure</h3>
<span class="nocode pln"> {Load Arguments, <a href="index.html#1:5">5</a>}</span>
<span class="nocode pln"> {Setup, <a href="index.html#1:12">12</a>}</span>

/* since exactly one condition flag should be set at any given time, set the Z flag */
reg[R_COND] = FL_ZRO;

/* set the PC to starting position */
/* 0x3000 is the default */
enum { PC_START = 0x3000 };
Expand Down Expand Up @@ -956,7 +959,7 @@ <h3> Store Register</h3>
<p>There is no specification for <em>how</em> trap routines must be implemented, only what they are supposed to do.
In our VM, we are going to do things slightly differently by writing them in C.
When a trap code is invoked, a C function will be called. When it is completed, execution will return to the instructions.
(If you are curious about trap codes in assembly, see <a href="https://github.com/rpendleton/lc3sim-c">Ryan's implementation</a>.)
(If you are curious about trap codes in assembly, see <a href="https://github.com/rpendleton/lc3sim-c">Ryan's implementation</a>.)
</p>
<p>Even though the trap routines can be written in assembly and this is what a physical LC-3 computer would do, it isn't the best fit for a VM. Instead of writing our own primitive I/O routines, we can take advantage of the ones available on our OS. This will make the VM run better on our computers, simplify the code, and provide a higher level of abstraction for portability.
</p>
Expand Down Expand Up @@ -1044,6 +1047,7 @@ <h3> PUTS</h3>
<pre class="prettyprint lang-c">
/* read a single ASCII char */
reg[R_R0] = (uint16_t)getchar();
update_flags(R_R0);
</pre>


Expand Down Expand Up @@ -1072,7 +1076,9 @@ <h3> PUTS</h3>
printf("Enter a character: ");
char c = getchar();
putc(c, stdout);
fflush(stdout);
reg[R_R0] = (uint16_t)c;
update_flags(R_R0);
}
</pre>

Expand Down Expand Up @@ -1704,7 +1710,7 @@ <h3> Debugging</h3>
<p>To list your own project, just make sure it is tagged with the GitHub topic <code>lc3</code>.
If your language is missing, feel free to submit a pull request.
</p>
<p>Special thanks to <a href="https://github.com/inkydragon">inkydragon</a> for contributing Windows platform support.
<p>Special thanks to <a href="https://github.com/inkydragon">inkydragon</a> for contributing Windows platform support.
</p>
<p>Want to contribute? We need help with an integration test.
This is a good <a href="https://github.com/justinmeiners/lc3-vm/issues/13">first issue</a> to learn from.
Expand Down
3 changes: 3 additions & 0 deletions docs/src/lc3-alt-win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ void ins(uint16_t instr)
/* TRAP GETC */
/* read a single ASCII char */
reg[R_R0] = (uint16_t)getchar();
update_flags(R_R0);

break;
case TRAP_OUT:
Expand Down Expand Up @@ -326,7 +327,9 @@ void ins(uint16_t instr)
printf("Enter a character: ");
char c = getchar();
putc(c, stdout);
fflush(stdout);
reg[R_R0] = (uint16_t)c;
update_flags(R_R0);
}

break;
Expand Down
5 changes: 5 additions & 0 deletions docs/src/lc3-alt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ void ins(uint16_t instr)
/* TRAP GETC */
/* read a single ASCII char */
reg[R_R0] = (uint16_t)getchar();
update_flags(R_R0);

break;
case TRAP_OUT:
Expand Down Expand Up @@ -334,7 +335,9 @@ void ins(uint16_t instr)
printf("Enter a character: ");
char c = getchar();
putc(c, stdout);
fflush(stdout);
reg[R_R0] = (uint16_t)c;
update_flags(R_R0);
}

break;
Expand Down Expand Up @@ -404,6 +407,8 @@ int main(int argc, const char* argv[])
disable_input_buffering();


reg[R_COND] = FL_ZRO;

enum { PC_START = 0x3000 };
reg[R_PC] = PC_START;

Expand Down
6 changes: 6 additions & 0 deletions docs/src/lc3-win.c
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,9 @@ int main(int argc, const char* argv[])
disable_input_buffering();


/* since exactly one condition flag should be set at any given time, set the Z flag */
reg[R_COND] = FL_ZRO;

/* set the PC to starting position */
/* 0x3000 is the default */
enum { PC_START = 0x3000 };
Expand Down Expand Up @@ -421,6 +424,7 @@ int main(int argc, const char* argv[])
/* TRAP GETC */
/* read a single ASCII char */
reg[R_R0] = (uint16_t)getchar();
update_flags(R_R0);

break;
case TRAP_OUT:
Expand Down Expand Up @@ -449,7 +453,9 @@ int main(int argc, const char* argv[])
printf("Enter a character: ");
char c = getchar();
putc(c, stdout);
fflush(stdout);
reg[R_R0] = (uint16_t)c;
update_flags(R_R0);
}

break;
Expand Down
6 changes: 6 additions & 0 deletions docs/src/lc3.c
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,9 @@ int main(int argc, const char* argv[])
disable_input_buffering();


/* since exactly one condition flag should be set at any given time, set the Z flag */
reg[R_COND] = FL_ZRO;

/* set the PC to starting position */
/* 0x3000 is the default */
enum { PC_START = 0x3000 };
Expand Down Expand Up @@ -428,6 +431,7 @@ int main(int argc, const char* argv[])
/* TRAP GETC */
/* read a single ASCII char */
reg[R_R0] = (uint16_t)getchar();
update_flags(R_R0);

break;
case TRAP_OUT:
Expand Down Expand Up @@ -456,7 +460,9 @@ int main(int argc, const char* argv[])
printf("Enter a character: ");
char c = getchar();
putc(c, stdout);
fflush(stdout);
reg[R_R0] = (uint16_t)c;
update_flags(R_R0);
}

break;
Expand Down
13 changes: 10 additions & 3 deletions index.lit
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,9 @@ int main(int argc, const char* argv[])
@{Load Arguments}
@{Setup}

/* since exactly one condition flag should be set at any given time, set the Z flag */
reg[R_COND] = FL_ZRO;

/* set the PC to starting position */
/* 0x3000 is the default */
enum { PC_START = 0x3000 };
Expand Down Expand Up @@ -653,7 +656,7 @@ The CPU executes the procedure's instructions, and when it is complete, the PC i
There is no specification for *how* trap routines must be implemented, only what they are supposed to do.
In our VM, we are going to do things slightly differently by writing them in C.
When a trap code is invoked, a C function will be called. When it is completed, execution will return to the instructions.
(If you are curious about trap codes in assembly, see [Ryan's implementation](https://github.com/rpendleton/lc3sim-c).)
(If you are curious about trap codes in assembly, see [Ryan's implementation](https://github.com/rpendleton/lc3sim-c).)

Even though the trap routines can be written in assembly and this is what a physical LC-3 computer would do, it isn't the best fit for a VM. Instead of writing our own primitive I/O routines, we can take advantage of the ones available on our OS. This will make the VM run better on our computers, simplify the code, and provide a higher level of abstraction for portability.

Expand Down Expand Up @@ -723,6 +726,7 @@ Input Character
--- TRAP GETC
/* read a single ASCII char */
reg[R_R0] = (uint16_t)getchar();
update_flags(R_R0);
---

Output Character
Expand All @@ -737,7 +741,9 @@ Prompt for Input Character
printf("Enter a character: ");
char c = getchar();
putc(c, stdout);
fflush(stdout);
reg[R_R0] = (uint16_t)c;
update_flags(R_R0);
}
---

Expand Down Expand Up @@ -1235,6 +1241,8 @@ int main(int argc, const char* argv[])
@{Load Arguments}
@{Setup}

reg[R_COND] = FL_ZRO;

enum { PC_START = 0x3000 };
reg[R_PC] = PC_START;

Expand Down Expand Up @@ -1321,8 +1329,7 @@ we decided to utilize GitHub tags to organize them.
To list your own project, just make sure it is tagged with the GitHub topic `lc3`.
If your language is missing, feel free to submit a pull request.

Special thanks to [inkydragon](https://github.com/inkydragon) for contributing Windows platform support.
Special thanks to [inkydragon](https://github.com/inkydragon) for contributing Windows platform support.

Want to contribute? We need help with an integration test.
This is a good [first issue](https://github.com/justinmeiners/lc3-vm/issues/13) to learn from.

0 comments on commit 90ba00c

Please sign in to comment.