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