Skip to content

Commit 5faa1cf

Browse files
zelenskiCS107E BOT
authored andcommitted
Lecture fb
commit d004e5324173f4fb0befa471fb53df1090e0cb33 Author: Julie Zelenski <[email protected]> Date: Mon Nov 4 10:01:42 2024 -0800 Lecture fb
1 parent 3ca9a7c commit 5faa1cf

File tree

14 files changed

+324
-3
lines changed

14 files changed

+324
-3
lines changed

_data/unreleased.csv

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ permalink,title,released
66
"/labs/projectlab1/","Lab 8: Project Team Meeting 1",false
77
"/labs/projectlab2/","Lab 9: Project Team Meeting 2",false
88
"/lectures/FloatingPoint_Sound/","Floating Point and Sound",false
9-
"/lectures/Framebuffer/","Graphics and the framebuffer",false
109
"/lectures/Guest/","Guest",false
1110
"/lectures/InfoSession/","Course Info Session",false
1211
"/lectures/Interrupts1/","Interrupts",false

lectures/Arithmetic/code/integers/main.c

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include <stdint.h>
22
#include "printf.h"
33
#include "uart.h"
4-
4+
#include "timer.h"
55
void bug_compare(void) {
66
int a = -20;
77
unsigned int b = 6;
@@ -12,6 +12,30 @@ void bug_compare(void) {
1212
printf("-20 >= 6 - omg \n");
1313
}
1414

15+
16+
unsigned int get_count() {
17+
static unsigned int count = 0xfffff000;
18+
return ++count;
19+
}
20+
21+
void delay_ticks(unsigned int us) {
22+
unsigned int enough = us * 24;
23+
unsigned int start = get_count();
24+
unsigned int sofar = 0;
25+
unsigned int stop_at = start + enough;
26+
unsigned int now = 0;
27+
int count = 0;
28+
//while (get_count() - start < enough) {
29+
while (get_count() < start + enough) {
30+
count++;
31+
//now = get_count();
32+
//sofar = now - start;
33+
//if (get_count() - start >= enough) break;
34+
//if (now >= stop_at) break;
35+
}
36+
printf("iterated %d times, start was %02x, delta enough %d delta sofar %d stopat %x now %x\n", count, start, enough, sofar, stop_at, now);
37+
}
38+
1539
void overflow(void) {
1640
for (unsigned char i = 0; i < 256; i++)
1741
printf("0x%2x\n", i);
@@ -41,5 +65,8 @@ void main(void) {
4165

4266
// overflow();
4367
// bug_compare();
44-
conversions();
68+
// conversions();
69+
for (int i = 0; i < 50; i++) {
70+
delay_ticks(5);
71+
}
4572
}

lectures/Framebuffer/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
released: true
3+
permalink: /lectures/Framebuffer/
4+
title: Graphics and the framebuffer
5+
readings: |
6+
+ [cdecl](https://cdecl.org) for unraveling C types
7+
+ Documentation for Mango Pi [Display Engine](readings/Allwinner_DE2.0_Spec_V1.0.pdf)
8+
+ [HDMI spec](https://hdmi.org/spec/index)
9+
---
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
NAME = clear
2+
3+
ARCH = -march=rv64im -mabi=lp64
4+
ASFLAGS = $(ARCH)
5+
CFLAGS = $(ARCH) -g -Og -I$$CS107E/include -Wall -ffreestanding
6+
LDFLAGS = -nostdlib -L$$CS107E/lib -T memmap.ld
7+
LDLIBS = -lmango -lmango_gcc
8+
9+
all : $(NAME).bin
10+
11+
%.bin: %.elf
12+
riscv64-unknown-elf-objcopy $< -O binary $@
13+
14+
%.elf: %.o
15+
riscv64-unknown-elf-gcc $(LDFLAGS) $^ $(LDLIBS) -o $@
16+
17+
%.o: %.c
18+
riscv64-unknown-elf-gcc $(CFLAGS) -c $< -o $@
19+
20+
%.o: %.s
21+
riscv64-unknown-elf-as $(ASFLAGS) $< -o $@
22+
23+
run: $(NAME).bin
24+
mango-run $<
25+
26+
clean:
27+
rm -f *.o *.bin *.elf *.list *~
28+
29+
.PHONY: all clean run
30+
.PRECIOUS: %.elf %.o
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Show how to clear the framebuffer memory accessing using different array layout
2+
3+
- `clear_char_by_char`: access as 1D array of unsigned char's
4+
- `clear_int_by_int`: access a 1D array of unsigned int's
5+
- `clear_2d`: access a 2D array of unsigned int's
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#include "fb.h"
2+
#include "de.h"
3+
#include "hdmi.h"
4+
#include "malloc.h"
5+
#include "printf.h"
6+
#include "uart.h"
7+
8+
// module-level variables
9+
static struct {
10+
int width; // count of horizontal pixels
11+
int height; // count of vertical pixels
12+
int depth; // num bytes per pixel
13+
void *framebuffer; // address of framebuffer memory
14+
} module;
15+
16+
17+
void fb_init(int width, int height, fb_mode_t mode) {
18+
module.width = width;
19+
module.height = height;
20+
module.depth = 4; // our pixels always 32-bit
21+
int nbytes = module.width * module.height * module.depth;
22+
module.framebuffer = malloc(nbytes);
23+
// what will be contents of newly malloc'ed memory block?
24+
25+
hdmi_resolution_id_t id = hdmi_best_match(width, height); // choose from available screen resolutions
26+
hdmi_init(id);
27+
de_init(width, height, hdmi_get_screen_width(), hdmi_get_screen_height());
28+
de_set_active_framebuffer(module.framebuffer);
29+
}
30+
31+
static void clear_char_by_char(void) {
32+
uint8_t *im = module.framebuffer;
33+
int total_bytes = module.width * module.height * module.depth;
34+
// write each byte in the framebuffer
35+
for (int i = 0; i < total_bytes; i++) {
36+
*im++ = 0xff; // white
37+
}
38+
}
39+
40+
static void clear_int_by_int_green(void) {
41+
unsigned int *im = module.framebuffer;
42+
int npixels = module.width * module.height;
43+
for (int i = 0; i < npixels; i++) {
44+
*im++ = 0xff00ff00; // green
45+
}
46+
}
47+
48+
static void clear_2d_purple(void) {
49+
unsigned int (*im)[module.width] = module.framebuffer;
50+
for (int y = 0; y < module.height; y++) {
51+
for (int x = 0; x < module.width; x++) {
52+
im[y][x] = 0xffff00ff; // purple
53+
}
54+
}
55+
}
56+
57+
static bool confirm(const char *msg) {
58+
printf("Next: %s. Hit any key when ready (q to quit): ", msg);
59+
int ch = uart_getchar();
60+
printf("%c\n", ch);
61+
return ch != 'q';
62+
}
63+
64+
void main(void) {
65+
uart_init();
66+
67+
fb_init(1600, 900, FB_SINGLEBUFFER);
68+
69+
printf("Screen size %d x %d\n", hdmi_get_screen_width(), hdmi_get_screen_height());
70+
printf("Framebuffer size %d x %d\n", module.width, module.height);
71+
clear_char_by_char();
72+
while (1) {
73+
if (!confirm("clear to white, char by char")) break;
74+
clear_char_by_char();
75+
76+
if (!confirm("clear to green, int by int")) break;
77+
clear_int_by_int_green();
78+
79+
if (!confirm("clear to purple, 2-D of int")) break;
80+
clear_2d_purple();
81+
}
82+
printf("Completed %s\n", __FILE__);
83+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
NAME = joshua
2+
3+
ARCH = -march=rv64im -mabi=lp64
4+
ASFLAGS = $(ARCH)
5+
CFLAGS = $(ARCH) -g -Og -I$$CS107E/include -Wall -ffreestanding
6+
LDFLAGS = -nostdlib -L$$CS107E/lib -T memmap.ld
7+
LDLIBS = -lmango -lmango_gcc
8+
9+
all : $(NAME).bin
10+
11+
%.bin: %.elf
12+
riscv64-unknown-elf-objcopy $< -O binary $@
13+
14+
%.elf: %.o
15+
riscv64-unknown-elf-gcc $(LDFLAGS) $^ $(LDLIBS) -o $@
16+
17+
%.o: %.c
18+
riscv64-unknown-elf-gcc $(CFLAGS) -c $< -o $@
19+
20+
%.o: %.s
21+
riscv64-unknown-elf-as $(ASFLAGS) $< -o $@
22+
23+
run: $(NAME).bin
24+
mango-run $<
25+
26+
clean:
27+
rm -f *.o *.bin *.elf *.list *~
28+
29+
.PHONY: all clean run
30+
.PRECIOUS: %.elf %.o
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include "console.h"
2+
#include "timer.h"
3+
4+
void main(void)
5+
{
6+
console_init(20, 32, GL_AMBER, GL_BLACK);
7+
8+
console_printf("GREETINGS PROFESSOR FALKEN\n");
9+
console_printf("\n");
10+
timer_delay_ms(2000);
11+
console_printf("HELLO\n");
12+
console_printf("\n");
13+
timer_delay_ms(2000);
14+
console_printf("A STRANGE GAME\n");
15+
timer_delay_ms(2000);
16+
console_printf("THE ONLY WINNING MOVE IS\n");
17+
timer_delay_ms(2000);
18+
console_printf("NOT TO PLAY\n");
19+
console_printf("\n");
20+
timer_delay_ms(2000);
21+
console_printf("HOW ABOUT A NICE GAME OF CHESS?\n");
22+
timer_delay_ms(2000);
23+
}
24+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
NAME = hello
2+
3+
ARCH = -march=rv64im -mabi=lp64
4+
ASFLAGS = $(ARCH)
5+
CFLAGS = $(ARCH) -g -Og -I$$CS107E/include -Wall -ffreestanding
6+
LDFLAGS = -nostdlib -L$$CS107E/lib -T memmap.ld
7+
LDLIBS = -lmango -lmango_gcc
8+
9+
all : $(NAME).bin
10+
11+
%.bin: %.elf
12+
riscv64-unknown-elf-objcopy $< -O binary $@
13+
14+
%.elf: %.o
15+
riscv64-unknown-elf-gcc $(LDFLAGS) $^ $(LDLIBS) -o $@
16+
17+
%.o: %.c
18+
riscv64-unknown-elf-gcc $(CFLAGS) -c $< -o $@
19+
20+
%.o: %.s
21+
riscv64-unknown-elf-as $(ASFLAGS) $< -o $@
22+
23+
run: $(NAME).bin
24+
mango-run $<
25+
26+
clean:
27+
rm -f *.o *.bin *.elf *.list *~
28+
29+
.PHONY: all clean run
30+
.PRECIOUS: %.elf %.o
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
When in single buffer mode, repeatedly clear and print string will produce
2+
a lot of jitter/tearing
3+
4+
Change to double buffer mode and there is smooth transition from
5+
one frame to another, neat!

0 commit comments

Comments
 (0)