UbixOS V2 2.0

tty.c
Go to the documentation of this file.
1 /*-
2 * Copyright (c) 2002-2018 The UbixOS Project.
3 * All rights reserved.
4 *
5 * This was developed by Christopher W. Olsen for the UbixOS Project.
6 *
7 * Redistribution and use in source and binary forms, with or without modification, are permitted
8 * provided that the following conditions are met:
9 *
10 * 1) Redistributions of source code must retain the above copyright notice, this list of
11 * conditions, the following disclaimer and the list of authors.
12 * 2) Redistributions in binary form must reproduce the above copyright notice, this list of
13 * conditions, the following disclaimer and the list of authors in the documentation and/or
14 * other materials provided with the distribution.
15 * 3) Neither the name of the UbixOS Project nor the names of its contributors may be used to
16 * endorse or promote products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
20 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
22 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <ubixos/tty.h>
30 #include <ubixos/kpanic.h>
31 #include <ubixos/spinlock.h>
32 #include <lib/kprintf.h>
33 #include <lib/kmalloc.h>
34 #include <sys/io.h>
35 #include <string.h>
36
37 static tty_term *terms = 0x0;
38 tty_term *tty_foreground = 0x0;
39 static struct spinLock tty_spinLock = SPIN_LOCK_INITIALIZER;
40
41 int tty_init() {
42 int i = 0x0;
43
44 /* Allocate memory for terminals */
45 terms = (tty_term *) kmalloc(sizeof(tty_term) * TTY_MAX_TERMS);
46 if (terms == 0x0)
47 kpanic("tty_init: Failed to allocate memory. File: %s, Line: %i\n", __FILE__, __LINE__);
48
49 /* Set up all default terminal information */
50 for (i = 0; i < TTY_MAX_TERMS; i++) {
51 terms[i].tty_buffer = (char *) kmalloc(80 * 60 * 2);
52 if (terms[i].tty_buffer == 0x0)
53 kpanic("tty_init: Failed to allocate buffer memory. File: %s, Line: %i\n", __FILE__, __LINE__);
54
55 terms[i].tty_pointer = terms[i].tty_buffer; /* Set up tty pointer to internal buffer */
56 terms[i].tty_x = 0x0; /* Set up default X position */
57 terms[i].tty_y = 0x0; /* Set up default Y position */
58 terms[i].tty_colour = 0x0A + i; /* Set up default tty text colour */
59 }
60
61 /* Read tty0 current position (to migrate from kprintf). */
62 outportByte(0x3D4, 0x0e);
63 terms[0].tty_y = inportByte(0x3D5);
64 outportByte(0x3D4, 0x0f);
65 terms[0].tty_x = inportByte(0x3D5);
66
67 /* Set up pointer for the foreground tty */
68 tty_foreground = &terms[0];
69
70 /* Set up the foreground ttys information */
71 tty_foreground->tty_pointer = (char *) 0xB8000;
72
73 /* Return to let kernel know initialization is complete */
74 kprintf("tty0 - Initialized\n");
75
76 return (0x0);
77 }
78
79 /*
80 This will change the specified tty. It ultimately copies the screen
81 to the foreground buffer copies the new ttys buffer to the screen and
82 adjusts a couple pointers and we are good to go.
83 */
84 int tty_change(uInt16 tty) {
85
86 if (tty > TTY_MAX_TERMS)
87 kpanic("Error: Changing to an invalid tty. File: %s, Line: %i\n", __FILE__, __LINE__);
88
89 /* Copy display buffer to tty buffer */
90 memcpy(tty_foreground->tty_buffer, (char *) 0xB8000, (80 * 60 * 2));
91
92 /* Copy new tty buffer to display buffer */
93 memcpy((char *) 0xB8000, terms[tty].tty_buffer, (80 * 60 * 2));
94
95 /*
96 Set the tty_pointer to the internal buffer so I can continue
97 writing to what it believes is the screen
98 */
99 tty_foreground->tty_pointer = tty_foreground->tty_buffer;
100
101 terms[tty].tty_pointer = (char *) 0xB8000;
102
103 /* set new foreground tty */
104 tty_foreground = &terms[tty];
105
106 /* Adjust cursor when we change consoles */
107 outportByte(0x3D4, 0x0F);
108 outportByte(0x3D5, tty_foreground->tty_x);
109 outportByte(0x3D4, 0x0E);
110 outportByte(0x3D5, tty_foreground->tty_y);
111
112 return (0x0);
113 }
114
115 int tty_print(char *string, tty_term *term) {
116 unsigned int bufferOffset = 0x0, character = 0x0, i = 0x0;
117 spinLock(&tty_spinLock);
118
119 /* We Need To Get The Y Position */
120 bufferOffset = term->tty_y;
121 bufferOffset <<= 8;
122
123 /* Then We Need To Add The X Position */
124 bufferOffset += term->tty_x;
125 bufferOffset <<= 1;
126
127 while ((character = *string++)) {
128 switch (character) {
129 case '\n':
130 bufferOffset = (bufferOffset / 160) * 160 + 160;
131 break;
132 default:
133 term->tty_pointer[bufferOffset++] = character;
134 term->tty_pointer[bufferOffset++] = term->tty_colour;
135 break;
136 } /* switch */
137
138 /* Check To See If We Are Out Of Bounds */
139 if (bufferOffset >= 160 * 25) {
140 for (i = 0; i < 160 * 24; i++) {
141 term->tty_pointer[i] = term->tty_pointer[i + 160];
142 }
143 for (i = 0; i < 80; i++) {
144 term->tty_pointer[(160 * 24) + (i * 2)] = 0x20;
145 term->tty_pointer[(160 * 24) + (i * 2) + 1] = term->tty_colour;
146 }
147 bufferOffset -= 160;
148 }
149 }
150
151 bufferOffset >>= 1; /* Set the new cursor position */
152 term->tty_x = (bufferOffset & 0xFF);
153 term->tty_y = (bufferOffset >> 0x8);
154
155 if (term == tty_foreground) {
156 outportByte(0x3D4, 0x0f);
157 outportByte(0x3D5, term->tty_x);
158 outportByte(0x3D4, 0x0e);
159 outportByte(0x3D5, term->tty_y);
160 }
161
162 spinUnlock(&tty_spinLock);
163
164 return (0x0);
165 }
166
167 tty_term *tty_find(uInt16 tty) {
168 return (&terms[tty]);
169 }
170
171 /***
172 END
173 ***/
spinlock.h
tty_find
tty_term * tty_find(uInt16 tty)
Definition: tty.c:167
tty_change
int tty_change(uInt16 tty)
Definition: tty.c:84
string.h
tty_init
int tty_init()
Definition: tty.c:41
uInt16
unsigned short int uInt16
Definition: objgfx30.h:48
tty_termNode::tty_buffer
char * tty_buffer
Definition: tty.h:37
tty_termNode::tty_x
uint16_t tty_x
Definition: tty.h:40
outportByte
void outportByte(unsigned int, unsigned char)
outputut one byte to specified port
Definition: io.c:72
spinUnlock
void spinUnlock(spinLock_t *lock)
Definition: spinlock.c:36
tty_termNode::tty_y
uint16_t tty_y
Definition: tty.h:41
SPIN_LOCK_INITIALIZER
#define SPIN_LOCK_INITIALIZER
Definition: spinlock.h:36
tty_termNode::tty_pointer
char * tty_pointer
Definition: tty.h:38
kpanic
void kpanic(const char *fmt,...)
print panic message and halt system
Definition: kpanic.c:41
memcpy
void * memcpy(const void *dst, const void *src, size_t length)
spinLock
void spinLock(spinLock_t *lock)
Definition: spinlock.c:55
tty_termNode
Definition: tty.h:36
inportByte
unsigned char inportByte(unsigned int)
input one byte from specified port
Definition: io.c:38
kpanic.h
TTY_MAX_TERMS
#define TTY_MAX_TERMS
Definition: tty.h:34
kprintf.h
tty_termNode::tty_colour
uint8_t tty_colour
Definition: tty.h:39
tty.h
tty_print
int tty_print(char *string, tty_term *term)
Definition: tty.c:115
tty_foreground
tty_term * tty_foreground
Definition: tty.c:38
kmalloc
void * kmalloc(uInt32 len)
Definition: kmalloc.c:241
spinLock
Definition: spinlock.h:41
io.h
kprintf
int kprintf(const char *,...)
Definition: kprintf.c:259
kmalloc.h