Skip to content
Browse files

support directing input and output to arbitrary pipes

  • Loading branch information...
1 parent 2ef2340 commit c75d4456d28a72e54ca34208053bbff46bba19fc @ghewgill committed Jun 9, 2009
Showing with 73 additions and 6 deletions.
  1. +10 −0 cpu.c
  2. +2 −0 cpu.h
  3. +39 −2 emulino.c
  4. +20 −4 usart.c
  5. +2 −0 usart.h
View
10 cpu.c
@@ -1270,6 +1270,16 @@ void cpu_load_eeprom(u8 *buf, u32 bufsize)
eeprom_load(buf, bufsize);
}
+void cpu_usart_set_output(int fd)
+{
+ usart_set_output(fd);
+}
+
+void cpu_usart_set_input(int fd)
+{
+ usart_set_input(fd);
+}
+
void cpu_reset()
{
PC = 0;
View
2 cpu.h
@@ -49,6 +49,8 @@ void out_pin(int pin, bool state);
void cpu_init();
void cpu_load_flash(u8 *buf, u32 bufsize);
void cpu_load_eeprom(u8 *buf, u32 bufsize);
+void cpu_usart_set_output(int fd);
+void cpu_usart_set_input(int fd);
void cpu_reset();
int cpu_run();
void cpu_set_pin(int pin, bool state);
View
41 emulino.c
@@ -18,8 +18,10 @@
* along with Emulino. If not, see <http://www.gnu.org/licenses/>.
*/
+#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
#include "cpu.h"
#include "loader.h"
@@ -44,10 +46,41 @@ int main(int argc, char *argv[])
exit(1);
}
+ int inf = 0;
+ int outf = 1;
+
+ int a = 1;
+ while (a < argc) {
+ if (argv[a][0] == '-') {
+ if (strcmp(argv[a], "-io") == 0) {
+ a++;
+ char fn[200];
+ snprintf(fn, sizeof(fn), "%s.in", argv[a]);
+ inf = open(fn, O_RDONLY);
+ if (inf == -1) {
+ perror(fn);
+ exit(1);
+ }
+ snprintf(fn, sizeof(fn), "%s.out", argv[a]);
+ outf = open(fn, O_WRONLY);
+ if (outf == -1) {
+ perror(fn);
+ exit(1);
+ }
+ } else {
+ fprintf(stderr, "Unknown option: %s\n", argv[a]);
+ exit(1);
+ }
+ } else {
+ break;
+ }
+ a++;
+ }
+
u8 prog[PROGRAM_SIZE_WORDS*2];
- u32 progsize = load_file(argv[1], prog, sizeof(prog));
+ u32 progsize = load_file(argv[a], prog, sizeof(prog));
if (progsize == 0) {
- perror(argv[1]);
+ perror(argv[a]);
exit(1);
}
@@ -57,6 +90,10 @@ int main(int argc, char *argv[])
cpu_init();
cpu_load_flash(prog, progsize);
cpu_load_eeprom(eeprom, eepromsize);
+
+ cpu_usart_set_input(inf);
+ cpu_usart_set_output(outf);
+
int i;
for (i = 0; i < 8; i++) {
cpu_pin_callback(PIN_PORTB+i, pinchange);
View
24 usart.c
@@ -22,6 +22,7 @@
#include <stdio.h>
#include <sys/select.h>
+#include <unistd.h>
#include "cpu.h"
@@ -37,6 +38,9 @@
#define USART_IRQ 19
+static int output = 0; // stdout
+static int input = 1; // stdin
+
u8 UCSRA;
u8 UCSRB;
@@ -66,24 +70,26 @@ u8 usart_read_udr(u16 addr)
{
if (UCSRA & USART_UCSRA_RXC) {
UCSRA &= ~USART_UCSRA_RXC;
- return getchar();
+ u8 c;
+ read(input, &c, 1);
+ return c;
} else {
return 0;
}
}
void usart_write_udr(u16 addr, u8 value)
{
- putchar(value);
+ write(output, &value, 1);
}
void usart_poll()
{
fd_set fds;
FD_ZERO(&fds);
- FD_SET(0, &fds);
+ FD_SET(input, &fds);
struct timeval timeout = {0, 0};
- if (select(1, &fds, NULL, NULL, &timeout) > 0) {
+ if (select(input+1, &fds, NULL, NULL, &timeout) > 0) {
UCSRA |= USART_UCSRA_RXC;
if (UCSRB & USART_UCSRB_RXCIE) {
irq(USART_IRQ);
@@ -98,3 +104,13 @@ void usart_init()
register_io(USART_UDR0, usart_read_udr, usart_write_udr);
register_poll(usart_poll);
}
+
+void usart_set_output(int f)
+{
+ output = f;
+}
+
+void usart_set_input(int f)
+{
+ input = f;
+}
View
2 usart.h
@@ -19,3 +19,5 @@
*/
void usart_init();
+void usart_set_output(int fd);
+void usart_set_input(int fd);

0 comments on commit c75d445

Please sign in to comment.
Something went wrong with that request. Please try again.