Mercurial > repos > blastem
annotate oscilloscope.c @ 2283:6f6f21d0c396
Fix missing address masks on some VRAM reads
author | Michael Pavone <pavone@retrodev.com> |
---|---|
date | Mon, 09 Jan 2023 00:15:26 -0800 |
parents | e27ab5f4bfe3 |
children | d30ea441b92e |
rev | line source |
---|---|
2245 | 1 #include "oscilloscope.h" |
2 #include "render.h" | |
3 #include "blastem.h" | |
4 | |
5 #define INVALID_TRIGGER 0xFFFFFFFF | |
6 #define WIDTH 1280 | |
7 #define HEIGHT 720 | |
8 | |
9 static void scope_window_close(uint8_t which) | |
10 { | |
11 if (current_system && current_system->toggle_debug_view) { | |
12 current_system->toggle_debug_view(current_system, DEBUG_OSCILLOSCOPE); | |
13 } | |
14 } | |
15 | |
16 oscilloscope *create_oscilloscope() | |
17 { | |
18 oscilloscope *scope = calloc(1, sizeof(oscilloscope)); | |
19 scope->channel_storage = 4; | |
20 scope->channels = calloc(scope->channel_storage, sizeof(scope_channel)); | |
21 scope->window = render_create_window("Oscilloscope", WIDTH, HEIGHT, scope_window_close); | |
22 return scope; | |
23 } | |
24 uint8_t scope_add_channel(oscilloscope *scope, const char *name, uint32_t sample_rate) | |
25 { | |
26 if (scope->num_channels == scope->channel_storage) { | |
27 scope->channel_storage *= 2; | |
28 scope->channels = realloc(scope->channels, scope->channel_storage * sizeof(scope_channel)); | |
29 } | |
30 uint8_t channel = scope->num_channels++; | |
31 scope_channel *chan = scope->channels + channel; | |
32 chan->name = name; | |
33 chan->period = sample_rate / 30; | |
34 chan->next_sample = 0; | |
35 chan->samples = calloc(chan->period, sizeof(int16_t)); | |
36 chan->last_trigger = INVALID_TRIGGER; | |
37 chan->cur_trigger = INVALID_TRIGGER; | |
38 return channel; | |
39 } | |
40 void scope_add_sample(oscilloscope *scope, uint8_t channel, int16_t value, uint8_t trigger) | |
41 { | |
42 if (channel >= scope->num_channels) { | |
43 return; | |
44 } | |
45 scope_channel *chan = scope->channels + channel; | |
46 if (trigger) { | |
47 chan->cur_trigger = chan->next_sample; | |
48 } | |
49 chan->samples[chan->next_sample++] = value; | |
50 if (chan->next_sample == chan->period) { | |
51 chan->next_sample = 0; | |
52 } | |
53 } | |
54 void scope_render(oscilloscope *scope) | |
55 { | |
56 int pitch; | |
57 uint32_t *fb = render_get_framebuffer(scope->window, &pitch); | |
58 memset(fb, 0, HEIGHT * pitch); | |
59 pitch /= sizeof(uint32_t); | |
60 int offset = 0; | |
61 int column_width = WIDTH/3; | |
62 int width = column_width * 3; | |
63 int row_height = HEIGHT / ((scope->num_channels + 2) / 3); | |
64 float value_scale = (float)row_height / 20000.0f; | |
65 uint32_t *cur_line = fb; | |
66 for (uint8_t i = 0; i < scope->num_channels; i++) | |
67 { | |
68 float samples_per_pixel = (float)scope->channels[i].period / (float)(2*column_width); | |
69 uint32_t start = scope->channels[i].last_trigger; | |
70 if (start == INVALID_TRIGGER) { | |
71 if (scope->channels[i].next_sample >= scope->channels[i].period / 2) { | |
72 start = scope->channels[i].next_sample - scope->channels[i].period / 2; | |
73 } else { | |
74 start = scope->channels[i].period - (scope->channels[i].period /2 - scope->channels[i].next_sample); | |
75 } | |
76 } | |
77 scope->channels[i].last_trigger = scope->channels[i].cur_trigger; | |
78 scope->channels[i].cur_trigger = INVALID_TRIGGER; | |
79 float cur_sample = start; | |
80 int last_y = -1; | |
81 for (int x = offset; x < offset + column_width; x++) | |
82 { | |
83 //TODO: bresenham | |
84 //TODO: at least linear filtering | |
85 int16_t sample = scope->channels[i].samples[(int)(cur_sample + 0.5f)]; | |
86 int y = (float)sample * value_scale + 0.5f; | |
2275
e27ab5f4bfe3
Fix off by one in oscilloscope rendering
Michael Pavone <pavone@retrodev.com>
parents:
2245
diff
changeset
|
87 if (y > row_height / 2 - 1) { |
e27ab5f4bfe3
Fix off by one in oscilloscope rendering
Michael Pavone <pavone@retrodev.com>
parents:
2245
diff
changeset
|
88 y = row_height / 2 - 1; |
2245 | 89 } else if (y < -(row_height / 2)) { |
90 y = -(row_height / 2); | |
91 } | |
92 y += row_height / 2; | |
93 if (last_y >= 0) { | |
94 int delta = last_y > y ? -1 : 1; | |
95 while (last_y != y) | |
96 { | |
97 cur_line[last_y * pitch + x ] = 0xFFFFFFFF; | |
98 last_y += delta; | |
99 } | |
100 } else { | |
101 last_y = y; | |
102 } | |
103 cur_line[y * pitch + x ] = 0xFFFFFFFF; | |
104 cur_sample += samples_per_pixel; | |
105 if (cur_sample + 0.5f >= scope->channels[i].period) { | |
106 cur_sample -= scope->channels[i].period; | |
107 } | |
108 } | |
2275
e27ab5f4bfe3
Fix off by one in oscilloscope rendering
Michael Pavone <pavone@retrodev.com>
parents:
2245
diff
changeset
|
109 |
2245 | 110 offset += column_width; |
111 if (offset >= width) { | |
112 offset = 0; | |
113 cur_line += pitch * row_height; | |
114 } | |
115 } | |
2275
e27ab5f4bfe3
Fix off by one in oscilloscope rendering
Michael Pavone <pavone@retrodev.com>
parents:
2245
diff
changeset
|
116 |
e27ab5f4bfe3
Fix off by one in oscilloscope rendering
Michael Pavone <pavone@retrodev.com>
parents:
2245
diff
changeset
|
117 |
2245 | 118 render_framebuffer_updated(scope->window, WIDTH); |
119 } | |
120 | |
121 void scope_close(oscilloscope *scope) | |
122 { | |
123 render_destroy_window(scope->window); | |
124 for (uint8_t i = 0; i < scope->num_channels; i++) | |
125 { | |
126 free(scope->channels[i].samples); | |
127 } | |
128 free(scope->channels); | |
129 free(scope); | |
130 } |