Skip to content

Commit 72724cc

Browse files
author
Steve White
committed
Splitting sqRpiMinimal.c into separate files.
1 parent a8610dd commit 72724cc

File tree

7 files changed

+520
-529
lines changed

7 files changed

+520
-529
lines changed

bcm2835/rules.mk

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@ MODULE_SRCS += \
77
$(LOCAL_DIR)/interrupts.c \
88
$(LOCAL_DIR)/mailbox.c \
99
$(LOCAL_DIR)/mmu.c \
10+
$(LOCAL_DIR)/sqRpiDisplay.c \
11+
$(LOCAL_DIR)/sqRpiHid.c \
12+
$(LOCAL_DIR)/sqRpiImage.c \
1013
$(LOCAL_DIR)/sqRpiMinimal.c \
1114
$(LOCAL_DIR)/sqRpiStubs.c \
15+
$(LOCAL_DIR)/sqRpiTimer.c \
1216
$(LOCAL_DIR)/start.s \
1317
$(LOCAL_DIR)/timer.c \
1418
$(LOCAL_DIR)/uart.c \

bcm2835/sqPlatformSpecific.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,4 @@ void * sqAllocateMemory(int minHeapSize, int desiredHeapSize);
2929
#undef ioLowResMSecs
3030
#undef ioMicroMSecs
3131

32+
#define ScreenDepth 32

bcm2835/sqRpiDisplay.c

Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
#include "sq.h"
2+
#include "framebuffer.h"
3+
#include "types.h"
4+
#include "barrier.h"
5+
6+
#define BytesPerComponent (ScreenDepth / 8)
7+
8+
int ioScreenSize(void) {
9+
return (fb_x << 16) | (fb_y & 0xFFFF); /* w is high 16 bits; h is low 16 bits */
10+
}
11+
12+
/*** Display stubs ***/
13+
unsigned char underCursorData[16 * 16 * BytesPerComponent];
14+
int lastCursorX, lastCursorY;
15+
16+
struct SqCursor {
17+
unsigned char size, depth, y, x;
18+
unsigned short data[16], mask[16];
19+
};
20+
21+
struct SqCursor SqueakCursor;
22+
23+
void hideCursor(void) {
24+
int bytesPerComponent = BytesPerComponent; //depth/8;
25+
int bytesPerRow = bytesPerComponent * fb_x;
26+
int rowLength = SqueakCursor.size * bytesPerComponent;
27+
28+
for (int y=0; y<SqueakCursor.size; y++) {
29+
int row = (lastCursorY + y) * bytesPerRow;
30+
int col = lastCursorX * bytesPerComponent;
31+
unsigned char *oldScreen = &underCursorData[y * rowLength];
32+
memcpy((unsigned char *)screenbase + row + col, oldScreen, rowLength);
33+
}
34+
}
35+
36+
void showCursor(void) {
37+
int bytesPerComponent = BytesPerComponent; //depth/8;
38+
int bytesPerRow = bytesPerComponent * fb_x;
39+
int rowLength = SqueakCursor.size * bytesPerComponent;
40+
41+
for (int y=0; y<SqueakCursor.size; y++) {
42+
int row = (lastCursorY + y) * bytesPerRow;
43+
int col = lastCursorX * bytesPerComponent;
44+
45+
unsigned char *rowData = (unsigned char *)screenbase + row + col;
46+
unsigned char *oldScreen = &underCursorData[y * rowLength];
47+
memcpy(oldScreen, rowData, rowLength);
48+
for (int x=SqueakCursor.size; x>0; x--) {
49+
int cursor = SqueakCursor.data[y] & 1<<x;
50+
if (cursor) {
51+
if (bytesPerComponent > 3) {
52+
*rowData++ = 0xff;
53+
}
54+
if (bytesPerComponent > 2) {
55+
*rowData++ = 0x00;
56+
}
57+
*rowData++ = 0x00;
58+
*rowData++ = 0x00;
59+
}
60+
else {
61+
rowData+=bytesPerComponent;
62+
}
63+
}
64+
}
65+
}
66+
67+
void moveCursor(int x, int y) {
68+
if (x == lastCursorX && y == lastCursorY) {
69+
return;
70+
}
71+
hideCursor();
72+
lastCursorX = x;
73+
lastCursorY = y;
74+
showCursor();
75+
}
76+
77+
int ioSetCursor(int cursorBitsIndex, int offsetX, int offsetY) {
78+
return ioSetCursorWithMask(cursorBitsIndex, 0, offsetX, offsetY);
79+
}
80+
81+
int ioSetCursorWithMask(int cursorBitsIndex, int cursorMaskIndex, int offsetX, int offsetY)
82+
{
83+
//printf("%s cursorBitsIndex=%i, cursorMaskIndex=%i, offsetX=%i, offsetY=%i\n", __PRETTY_FUNCTION__, cursorBitsIndex, cursorMaskIndex, offsetX, offsetY);
84+
85+
hideCursor();
86+
87+
/* Set the 16x16 cursor bitmap. If cursorMaskIndex is nil, then make the mask the same as
88+
the cursor bitmap. If not, then mask and cursor bits combined determine how cursor is
89+
displayed:
90+
mask cursor effect
91+
0 0 transparent (underlying pixel shows through)
92+
1 1 opaque black
93+
1 0 opaque white
94+
0 1 invert the underlying pixel
95+
*/
96+
SqueakCursor.size = 16;
97+
SqueakCursor.depth = 1;
98+
SqueakCursor.x = -offsetX;
99+
SqueakCursor.y = -offsetY;
100+
101+
for (int i = 0; i < SqueakCursor.size; i++) {
102+
SqueakCursor.data[i] = (checkedLongAt(cursorBitsIndex + (4 * i)) >> 16) & 0xFFFF;
103+
104+
if (cursorMaskIndex == 0) {
105+
SqueakCursor.mask[i] = SqueakCursor.data[i];
106+
}
107+
else {
108+
SqueakCursor.mask[i] = (checkedLongAt(cursorMaskIndex + (4 * i)) >> 16) & 0xFFFF;
109+
}
110+
}
111+
112+
showCursor();
113+
return true;
114+
}
115+
116+
int ioSetDisplayMode(int width, int height, int depth, int fullscreenFlag) {
117+
printf("%s width=%i, height=%i, depth=%i, fullscreenFlag=%i\n", __PRETTY_FUNCTION__, width, height, depth, fullscreenFlag);
118+
return 0;
119+
}
120+
121+
int ioSetFullScreen(int fullScreen) {
122+
printf("%s fullScreen=%i\n", __PRETTY_FUNCTION__, fullScreen);
123+
return 0;
124+
}
125+
126+
int ioHasDisplayDepth(int depth) {
127+
printf("%s depth=%i\n", __PRETTY_FUNCTION__, depth);
128+
if (depth == ScreenDepth) {
129+
return true;
130+
}
131+
return false;
132+
}
133+
134+
int ioShowDisplay(
135+
int dispBitsIndex, int width, int height, int depth,
136+
int affectedL, int affectedR, int affectedT, int affectedB)
137+
{
138+
if(affectedR <= affectedL || affectedT >= affectedB) {
139+
return 1;
140+
}
141+
142+
bool clippedCursor = false;
143+
if ((lastCursorX >= affectedL || lastCursorX + SqueakCursor.size >= affectedL) && (lastCursorX <= affectedR || lastCursorX + SqueakCursor.size <= affectedR) &&
144+
(lastCursorY >= affectedT || lastCursorY + SqueakCursor.size >= affectedT) && (lastCursorY <= affectedB || lastCursorY + SqueakCursor.size <= affectedB))
145+
{
146+
clippedCursor = true;
147+
}
148+
149+
if (clippedCursor == true) {
150+
hideCursor();
151+
}
152+
153+
int bytesPerComponent = depth/8;
154+
int rowLength = (affectedR - affectedL) * bytesPerComponent;
155+
156+
int x,y;
157+
int sourceRow, sourceCol;
158+
int destRow, destCol;
159+
160+
for (y = affectedT; y < affectedB; y++) {
161+
destRow = y * bytesPerComponent * fb_x;
162+
destCol = affectedL * bytesPerComponent;
163+
164+
sourceRow = y * bytesPerComponent * width;
165+
sourceCol = affectedL * bytesPerComponent;
166+
#if 1
167+
if (bytesPerComponent == 4) {
168+
uint32_t *toData = (uint32_t*)(screenbase + destRow + destCol);
169+
uint32_t *fromData = (uint32_t *)(dispBitsIndex + sourceRow + sourceCol);
170+
for (x = affectedL; x<affectedR; x++) {
171+
*toData =
172+
0xff << 24 | // alpha
173+
(((*fromData ) & 0xff) << 16) | // blue
174+
(((*fromData >> 8) & 0xff) << 8) | // green
175+
(((*fromData >> 16) & 0xff) ) ; // red
176+
toData++,fromData++;
177+
}
178+
}
179+
else if (bytesPerComponent == 2) {
180+
uint16_t *toData = (uint16_t*)(screenbase + destRow + destCol);
181+
uint16_t *fromData = (uint16_t *)(dispBitsIndex + sourceRow + sourceCol);
182+
183+
// Use 16 bits to store each pixel:
184+
// the first 5 bit representing the intensity of the red channel
185+
// the next 6 bits representing the intensity of the green channel and
186+
// the final 5 bits representing the intensity of the blue channel.
187+
for (x = affectedL; x<affectedR; x++) {
188+
*toData =
189+
(((((*fromData & 0x7C00) >> 10) << 3) >> 3) << 11) | // Red
190+
(((((*fromData & 0x3E0) >> 5) << 3) >> 2) << 5) | // Green
191+
(((((*fromData & 0x1F) ) << 3) >> 3) << 0); // Blue
192+
193+
toData++,fromData++;
194+
}
195+
}
196+
#else
197+
memcpy((unsigned char *)screenbase + row + col, (unsigned char *)dispBitsIndex + row + col, rowLength);
198+
#endif
199+
}
200+
201+
if (clippedCursor == true) {
202+
showCursor();
203+
}
204+
dmb();
205+
success(true);
206+
}
207+
208+
int ioForceDisplayUpdate(void) {
209+
return true;
210+
}
211+
212+
int ioFormPrint(int bitsAddr, int width, int height, int depth, double hScale, double vScale, int landscapeFlag) {
213+
/* experimental: print a form with the given bitmap, width, height, and depth at
214+
the given horizontal and vertical scales in the given orientation */
215+
216+
printf("%s\n", __PRETTY_FUNCTION__);
217+
success(false);
218+
}

bcm2835/sqRpiHid.c

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
#include "sq.h"
2+
#include "keyboard.h"
3+
#include "mouse.h"
4+
#include "timer.h" // watchdog
5+
#include "usbd.h"
6+
7+
/*** Variables -- Event Recording ***/
8+
#define KEYBUF_SIZE 64
9+
int keyBuf[KEYBUF_SIZE]; /* circular buffer */
10+
int keyBufGet = 0; /* index of next item of keyBuf to read */
11+
int keyBufPut = 0; /* index of next item of keyBuf to write */
12+
int keyBufOverflows = 0; /* number of characters dropped */
13+
14+
/*** Variables -- Imported from Virtual Machine ***/
15+
extern int interruptCheckCounter;
16+
extern int interruptKeycode;
17+
extern int interruptPending; /* set to true by RecordKeystroke if interrupt key is pressed */
18+
19+
void RecordKeystroke(unsigned char key, struct KeyboardModifiers modifiers) {
20+
int keystate;
21+
22+
/* keystate: low byte is the ascii character; next 4 bits are modifier bits */
23+
int keyModifier = 0;
24+
if (modifiers.LeftControl || modifiers.RightControl) {
25+
keyModifier = 1 << 9;
26+
}
27+
if (modifiers.LeftShift || modifiers.RightShift) {
28+
keyModifier = 1 << 8;
29+
}
30+
if (modifiers.LeftAlt || modifiers.RightAlt) {
31+
keyModifier = 1 << 10;
32+
}
33+
if (modifiers.LeftGui || modifiers.RightGui) {
34+
keyModifier = 1 << 11;
35+
}
36+
keystate = (keyModifier) | (key & 0xff);
37+
38+
if (keystate == interruptKeycode) {
39+
/* Note: interrupt key is "meta"; it not reported as a keystroke */
40+
interruptPending = true;
41+
interruptCheckCounter = 0;
42+
} else {
43+
keyBuf[keyBufPut] = keystate;
44+
keyBufPut = (keyBufPut + 1) % KEYBUF_SIZE;
45+
if (keyBufGet == keyBufPut) {
46+
/* buffer overflow; drop the last character */
47+
keyBufGet = (keyBufGet + 1) % KEYBUF_SIZE;
48+
keyBufOverflows++;
49+
}
50+
}
51+
}
52+
53+
int ioGetKeystrokeAndAdvance(bool advance) {
54+
/* return the next keystroke from the buffer or -1 if the buffer is empty */
55+
int keystate;
56+
57+
// ioProcessEvents(); /* process all pending events */
58+
if (keyBufGet == keyBufPut) {
59+
return -1; /* keystroke buffer is empty */
60+
} else {
61+
keystate = keyBuf[keyBufGet];
62+
if (advance == true) {
63+
keyBufGet = (keyBufGet + 1) % KEYBUF_SIZE;
64+
}
65+
/* set modifer bits in buttonState to reflect the last keystroke fetched */
66+
// buttonState = ((keystate >> 5) & 0xF8) | (buttonState & 0x7);
67+
}
68+
return keystate;
69+
}
70+
71+
int ioGetKeystroke(void) {
72+
return ioGetKeystrokeAndAdvance(true);
73+
}
74+
75+
int ioPeekKeystroke(void) {
76+
return ioGetKeystrokeAndAdvance(false);
77+
}
78+
79+
80+
int ioProcessEvents(void) {
81+
//printf("%s\n", __PRETTY_FUNCTION__);
82+
int keyboardCount = KeyboardCount();
83+
int mouseCount = MouseCount();
84+
if (keyboardCount == 0 || mouseCount == 0) {
85+
UsbCheckForChange();
86+
keyboardCount = KeyboardCount();
87+
mouseCount = MouseCount();
88+
}
89+
90+
for (int kbdIndex=0; kbdIndex<keyboardCount; kbdIndex++) {
91+
uint32_t keyboard = KeyboardGetAddress(kbdIndex);
92+
int result = KeyboardPoll(keyboard);
93+
if (result != OK) {
94+
continue;
95+
}
96+
97+
int keysDown = KeyboardGetKeyDownCount(keyboard);
98+
if (keysDown == 0) {
99+
continue;
100+
}
101+
102+
struct KeyboardModifiers modifiers = KeyboardGetModifiers(keyboard);
103+
for (int i=0; i<keysDown; i++) {
104+
int keyCode = KeyboardGetKeyDown(keyboard, i);
105+
RecordKeystroke((modifiers.LeftShift || modifiers.RightShift ? KeysShift[keyCode] : KeysNormal[keyCode]), modifiers);
106+
}
107+
}
108+
109+
for (int mouseIndex=0; mouseIndex<mouseCount; mouseIndex++) {
110+
uint32_t mouse = MouseGetAddress(mouseIndex);
111+
MousePoll(mouse);
112+
//int position = MouseGetPosition(mouse);
113+
//printf("mouse: x=%i, y=%i\n", ((position>>16)&0xffff), (position&0xffff));
114+
}
115+
116+
watchdog_reset();
117+
118+
return true;
119+
}
120+
121+
122+
int ioMousePoint(void) {
123+
int mouseCount = MouseCount();
124+
if (mouseCount > 0) {
125+
uint32_t mouse = MouseGetAddress(0);
126+
int point = MouseGetPosition(mouse);
127+
moveCursor((point >> 16) & 0xffff, point & 0xffff);
128+
return point;
129+
}
130+
int x=0, y=0;
131+
return (x << 16) | (y & 0xFFFF); /* x is high 16 bits; y is low 16 bits */
132+
}
133+
134+
int ioGetButtonState(void) {
135+
int result = 0;
136+
int mouseCount = MouseCount();
137+
if (mouseCount > 0) {
138+
uint32_t mouse = MouseGetAddress(0);
139+
int buttonState = MouseGetButtons(mouse);
140+
141+
if (buttonState & 0x01) {
142+
result |= 1 << 2;
143+
}
144+
if (buttonState & 0x02) {
145+
result |= 1 << 0;
146+
}
147+
if (buttonState & 0x04) {
148+
result |= 1 << 1;
149+
}
150+
}
151+
152+
return result;
153+
}

0 commit comments

Comments
 (0)