From 94268018dd496e5f138843c053048ba4e2e0c701 Mon Sep 17 00:00:00 2001
From: Bingzheng Feng <b26feng@uwaterloo.ca>
Date: Sun, 6 May 2018 19:20:25 -0400
Subject: [PATCH] clock and q works at same time

---
 include/cmd.h       |  1 +
 include/myio.h      |  8 ++++++++
 src/Makefile        |  2 +-
 src/libsrc/Makefile | 10 +++++++++-
 src/libsrc/cmd.c    | 19 +++++++++++--------
 src/libsrc/myio.c   | 24 ++++++++++++++++++++++++
 6 files changed, 54 insertions(+), 10 deletions(-)
 create mode 100644 include/myio.h
 create mode 100644 src/libsrc/myio.c

diff --git a/include/cmd.h b/include/cmd.h
index 089a981..8c6b698 100644
--- a/include/cmd.h
+++ b/include/cmd.h
@@ -2,5 +2,6 @@
 #define __CMD_H__
 
 #define CMD_HALT 0
+#define CMD_NOINPUT 1
 int ListenCommand();
 #endif
diff --git a/include/myio.h b/include/myio.h
new file mode 100644
index 0000000..fec4dfc
--- /dev/null
+++ b/include/myio.h
@@ -0,0 +1,8 @@
+#ifndef __MYIO_H__
+#define __MYIO_H__
+
+#define COM1 0
+#define COM2 1
+
+int mygetc(int Channel,char* Char);
+#endif
diff --git a/src/Makefile b/src/Makefile
index 38247bf..e58a8ea 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -28,7 +28,7 @@ startup.o: startup.s
 	$(AS) $(ASFLAGS) -o startup.o startup.s
 
 startup.elf: startup.o
-	$(LD) $(LDFLAGS) -o $@ startup.o -lmycanvas -lmyclock -lcmd -lbwio -lgcc
+	$(LD) $(LDFLAGS) -o $@ startup.o -lmycanvas -lmyclock -lcmd -lmyio -lbwio -lgcc
 
 clocktest.s: clocktest.c
 	$(XCC) -S $(CFLAGS) clocktest.c
diff --git a/src/libsrc/Makefile b/src/libsrc/Makefile
index 774204d..83c4cc1 100644
--- a/src/libsrc/Makefile
+++ b/src/libsrc/Makefile
@@ -29,6 +29,8 @@ mycanvas.s: mycanvas.c
 myclock.s: myclock.c
 	$(XCC) -S $(CFLAGS) myclock.c
 
+myio.s: myio.c
+	$(XCC) -S $(CFLAGS) myio.c
 
 bwio.o: bwio.s
 	$(AS) $(ASFLAGS) -o bwio.o bwio.s
@@ -42,6 +44,9 @@ mycanvas.o: mycanvas.s
 myclock.o: myclock.s
 	$(AS) $(ASFLAGS) -o myclock.o myclock.s
 
+myio.o: myio.s
+	$(AS) $(ASFLAGS) -o myio.o myio.s
+
 bwio.a: bwio.o
 	$(AR) $(ARFLAGS) $@ bwio.o
 
@@ -53,12 +58,15 @@ mycanvas.a: mycanvas.o
 
 myclock.a: myclock.o
 	$(AR) $(ARFLAGS) $@ myclock.o
+myio.a: myio.o
+	$(AR) $(ARFLAGS) $@ myio.o
 
-install: bwio.a mycanvas.a myclock.a cmd.a
+install: bwio.a mycanvas.a myclock.a cmd.a myio.a
 	mv bwio.a ../../lib/libbwio.a
 	mv cmd.a ../../lib/libcmd.a
 	mv mycanvas.a ../../lib/libmycanvas.a
 	mv myclock.a ../../lib/libmyclock.a
+	mv myio.a ../../lib/libmyio.a
 
 clean:
 	-rm -f *.s *.o *.a *~ ../../lib/*.a
diff --git a/src/libsrc/cmd.c b/src/libsrc/cmd.c
index 22ade43..2aa0f8d 100644
--- a/src/libsrc/cmd.c
+++ b/src/libsrc/cmd.c
@@ -1,15 +1,18 @@
-#include <bwio.h>
+#include <myio.h>
 #include <cmd.h>
 #include <mycanvas.h>
 
 int ListenCommand()
 {
   CURSOR_POS(20,1);
-  char FirstChar = bwgetc(COM2);
-  switch(FirstChar){
-  case 'q':
-    return CMD_HALT;
-  default:
-    break;
-  }
+  char FirstChar;
+  if(mygetc(COM2, &FirstChar)){ 
+    switch(FirstChar){
+    case 'q':
+      return CMD_HALT;
+    default:
+      break;
+    }
+  } 
+  return CMD_NOINPUT;
 }
diff --git a/src/libsrc/myio.c b/src/libsrc/myio.c
new file mode 100644
index 0000000..17c293a
--- /dev/null
+++ b/src/libsrc/myio.c
@@ -0,0 +1,24 @@
+#include <myio.h>
+#include <ts7200.h>
+
+int mygetc(int Channel, char *Char){
+  int *flag, *data;
+  switch (Channel){
+  case COM1:
+    flag = (int *)(UART1_BASE + UART_FLAG_OFFSET);
+    data = (int *)(UART1_BASE + UART_DATA_OFFSET);
+    break;
+  case COM2:
+    flag = (int *)(UART2_BASE + UART_FLAG_OFFSET);
+    data = (int *)(UART2_BASE + UART_DATA_OFFSET);
+    break;
+  default:
+    break;
+  }
+  if( *flag & RXFF_MASK){
+    *Char = (char) *data;
+    return 1;
+  }else{
+    return 0;
+  }
+}
-- 
GitLab