From 9dc65f84b480d5869265428c1957c50bdee3a1f5 Mon Sep 17 00:00:00 2001
From: alekseiplusplus <alekseijeaves@protonmail.com>
Date: Wed, 29 Nov 2023 19:28:04 +1100
Subject: Display progress, still wip

---
 src/apple.c           |  6 ++--
 src/main.c            |  8 ++---
 src/video/interface.h |  4 +--
 src/video/ncurses.c   | 90 +++++++++++++++++++++++++++++++++++----------------
 4 files changed, 72 insertions(+), 36 deletions(-)

diff --git a/src/apple.c b/src/apple.c
index 1e82d0d..ec7769a 100644
--- a/src/apple.c
+++ b/src/apple.c
@@ -23,7 +23,7 @@ void AppleReset(){
 	Memory = calloc(MEMORY_SIZE, sizeof(byte));
 }
 
-byte ToAppleAscii(char x)
+/*byte ToAppleAscii(char x)
 {
 	if (x < 0x20 || x >= 0x60)
 		return -1;
@@ -37,7 +37,7 @@ byte ToAscii(char x)
 	if (x < 0x20)
 		x += 0x40;
 	return x;
-}
+}*/
 
 
 byte GetMemory(address x){
@@ -46,7 +46,7 @@ byte GetMemory(address x){
 		// I opted to make the kbd return successfully at all times for the sake of simplicity.
 		// This is not technically "accurate" behavior however.
 	case KBD:
-		return 0b10000000 | ToAppleAscii(UserInput());
+		return 0b10000000 | UserInput();
 	case KBD_CR: case DSP:
 		return 0b10000000;
 	default:
diff --git a/src/main.c b/src/main.c
index 6b235bb..ef09933 100644
--- a/src/main.c
+++ b/src/main.c
@@ -8,11 +8,11 @@ int main() {
 
 	AppleOn();
 
-	//while(1) {
-	//	UserInput();
-	//}
+	while(1) {
+		TerminalInput( UserInput() );
+	}
 
 	TerminalClose();
 
 	return 0;
-}
\ No newline at end of file
+}
diff --git a/src/video/interface.h b/src/video/interface.h
index e3244b0..048aa25 100644
--- a/src/video/interface.h
+++ b/src/video/interface.h
@@ -7,7 +7,7 @@
 #include <stdlib.h>
 
 // Common procedure for taking in user input.
-char UserInput();
+byte UserInput();
 
 // Initialization procedure for the terminal
 void TerminalInit();
@@ -16,4 +16,4 @@ void TerminalInit();
 void TerminalClose();
 
 // Deliver an Apple ASCII character to the terminal.
-void TerminalInput(char n);
\ No newline at end of file
+void TerminalInput(byte n);
diff --git a/src/video/ncurses.c b/src/video/ncurses.c
index 2e3e519..d584088 100644
--- a/src/video/ncurses.c
+++ b/src/video/ncurses.c
@@ -14,24 +14,43 @@ int TermY = 0;
 WINDOW *AppleWindow;
 
 
-char UserInput()
+byte UserInput()
 {
-	return getch();
+	int c = getch();
+	
+	switch(c)
+	{
+	// Convert special characters
+	case KEY_BACKSPACE:
+		return 0xDF;
+	case KEY_ENTER:
+		return 0x8D;
+	case KEY_EXIT: //TODO: Figure out if this is Esc or not.
+		return 0x9B;
+	// Convert regular characters
+	default:
+		if (c < 0x20 || c >= 0x60)
+			return -1;
+		if (c >= 0x40)
+			c -= 0x40;
+		break;
+	}
+
+	return c;
 }
 
 
 
 void TerminalInit()
 {
+	// ncurses initialization functions.
     initscr();
     cbreak();
     noecho();
     curs_set(0);
     keypad(stdscr, TRUE);
 
-    AppleWindow = newwin(24, 40, 1, 1);
-
-    // Draw the border.
+    // Draw decorative border.
     attron(A_REVERSE);
     move(0, 0);
     printw("                                          ");
@@ -45,9 +64,13 @@ void TerminalInit()
     mvprintw(27, 0, "                                          ");
     attroff(A_REVERSE);
 
-
+	// Create seperate Apple screen window.
+    AppleWindow = newwin(24, 40, 1, 1);
+	
+	// @ prompt.
     mvwaddch(AppleWindow, TermY, TermX, '@' | A_BLINK);
 
+	// Initialize the terminal shift register variables.
     TerminalShiftRegister = (byte*)malloc(960);
     TerminalShiftRegisterPosition = TerminalShiftRegister;
     TerminalShiftRegisterOffset = 0;
@@ -60,60 +83,73 @@ void TerminalInit()
 void TerminalClose()
 {
     free(TerminalShiftRegister);
-    curs_set(1);
+	curs_set(1);
     endwin();
 }
 
 
 
-// Takes an an Apple I ASCII character.
-void TerminalInput(char n)
+void TerminalInput(byte n)
 {
+	// Handle special characters and conversion.
+	switch (n)
+	{
+	case 0xDF: // Backslash
+	case 0x8D: // Enter
+	case 0x9B: // Escape
+		return;
+	default:
+		// Convert to ASCII
+		if (n < 0x20) n += 0x40;
+	}
+	
+	// Place character
     mvwaddch(AppleWindow, TermY, TermX, ' ');
     mvwaddch(AppleWindow, TermY, TermX, n);
-
+	
+	// Add character to register
     *TerminalShiftRegisterPosition = n;
     TerminalShiftRegisterPosition++;
     if (TerminalShiftRegisterPosition >= (TerminalShiftRegister + 960))
         TerminalShiftRegisterPosition = TerminalShiftRegister;
     
-
     TermX++;
-
-    //if (n == KEY_ENTER) {
-    //    TermY++;
-    //}
-
+	// If X is past width..
     if (TermX >= 40) {
+		//.. move to start of next line.
         TermX = 0;
         TermY++;
     }
 
+	// If Y is past height..
     if (TermY >= 24) {
-        TerminalShiftRegisterOffset += 40; // Discard first line
-        if (TerminalShiftRegisterOffset >= 960)
+		//.. discard the first line..
+		TerminalShiftRegisterOffset += 40;
+
+		//.. verify the register offset..
+		if (TerminalShiftRegisterOffset >= 960)
             TerminalShiftRegisterOffset = TerminalShiftRegister;
 
+		//.. and create an offset variable to cycle through memory with.
         byte *offset = TerminalShiftRegister + TerminalShiftRegisterOffset;
 
-        // For every position, fill with contents of the terminal shift register
+        // Then, for every cell of the screen, fill with the contents of the video memory.
         for (int i = 0; i < 23; i++) {
         for (int j = 0; j < 40; j++) {
-
             if (offset >= (TerminalShiftRegister + 960))
-                offset -= 960;
-            
-            mvwaddch(AppleWindow, i, j, ToAscii(*(offset)));
+                offset -= TerminalShiftRegister;
+            mvwaddch(AppleWindow, i, j, (*offset < 0x20) ? *offset + 0x40 : *offset );
             offset++;
         }}
-
+		
+		// Set to start of final line, and clear line.
         TermY = 23;
         TermX = 0;
-
-        // Clear bottom line.
-        mvwprintw(AppleWindow, TermY, TermX, "                                        ");
+        mvwprintw(AppleWindow, TermY, TermX,
+			"                                        ");
     }
 
+	// @ prompt.
     mvwaddch(AppleWindow, TermY, TermX, '@' | A_BLINK);
     wrefresh(AppleWindow);
 }
-- 
cgit v1.2.3