Mercurial > repos > blastem
annotate oscilloscope.c @ 2688:b42f00a3a937 default tip
Fix default target. Ensure m68k.h and z80.h are built before anything else when no dep info is available
author | Michael Pavone <pavone@retrodev.com> |
---|---|
date | Mon, 31 Mar 2025 21:06:18 -0700 |
parents | da2e06c42d16 |
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; | |
2685
da2e06c42d16
Add a compile-time flag to use RGB565 instead of ABGR/ARGB
Michael Pavone <pavone@retrodev.com>
parents:
2301
diff
changeset
|
59 pixel_t *fb = render_get_framebuffer(scope->window, &pitch); |
2245 | 60 memset(fb, 0, HEIGHT * pitch); |
2685
da2e06c42d16
Add a compile-time flag to use RGB565 instead of ABGR/ARGB
Michael Pavone <pavone@retrodev.com>
parents:
2301
diff
changeset
|
61 pitch /= sizeof(pixel_t); |
2245 | 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; | |
2685
da2e06c42d16
Add a compile-time flag to use RGB565 instead of ABGR/ARGB
Michael Pavone <pavone@retrodev.com>
parents:
2301
diff
changeset
|
67 pixel_t *cur_line = fb; |
da2e06c42d16
Add a compile-time flag to use RGB565 instead of ABGR/ARGB
Michael Pavone <pavone@retrodev.com>
parents:
2301
diff
changeset
|
68 pixel_t white = render_map_color(255, 255, 255); |
2245 | 69 for (uint8_t i = 0; i < scope->num_channels; i++) |
70 { | |
71 float samples_per_pixel = (float)scope->channels[i].period / (float)(2*column_width); | |
72 uint32_t start = scope->channels[i].last_trigger; | |
73 if (start == INVALID_TRIGGER) { | |
74 if (scope->channels[i].next_sample >= scope->channels[i].period / 2) { | |
75 start = scope->channels[i].next_sample - scope->channels[i].period / 2; | |
76 } else { | |
77 start = scope->channels[i].period - (scope->channels[i].period /2 - scope->channels[i].next_sample); | |
78 } | |
79 } | |
80 scope->channels[i].last_trigger = scope->channels[i].cur_trigger; | |
81 scope->channels[i].cur_trigger = INVALID_TRIGGER; | |
82 float cur_sample = start; | |
83 int last_y = -1; | |
84 for (int x = offset; x < offset + column_width; x++) | |
85 { | |
86 //TODO: bresenham | |
87 //TODO: at least linear filtering | |
88 int16_t sample = scope->channels[i].samples[(int)(cur_sample + 0.5f)]; | |
89 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
|
90 if (y > row_height / 2 - 1) { |
e27ab5f4bfe3
Fix off by one in oscilloscope rendering
Michael Pavone <pavone@retrodev.com>
parents:
2245
diff
changeset
|
91 y = row_height / 2 - 1; |
2245 | 92 } else if (y < -(row_height / 2)) { |
93 y = -(row_height / 2); | |
94 } | |
95 y += row_height / 2; | |
96 if (last_y >= 0) { | |
97 int delta = last_y > y ? -1 : 1; | |
98 while (last_y != y) | |
99 { | |
2685
da2e06c42d16
Add a compile-time flag to use RGB565 instead of ABGR/ARGB
Michael Pavone <pavone@retrodev.com>
parents:
2301
diff
changeset
|
100 cur_line[last_y * pitch + x ] = white; |
2245 | 101 last_y += delta; |
102 } | |
103 } else { | |
104 last_y = y; | |
105 } | |
2685
da2e06c42d16
Add a compile-time flag to use RGB565 instead of ABGR/ARGB
Michael Pavone <pavone@retrodev.com>
parents:
2301
diff
changeset
|
106 cur_line[y * pitch + x ] = white; |
2245 | 107 cur_sample += samples_per_pixel; |
108 if (cur_sample + 0.5f >= scope->channels[i].period) { | |
109 cur_sample -= scope->channels[i].period; | |
110 } | |
111 } | |
2275
e27ab5f4bfe3
Fix off by one in oscilloscope rendering
Michael Pavone <pavone@retrodev.com>
parents:
2245
diff
changeset
|
112 |
2245 | 113 offset += column_width; |
114 if (offset >= width) { | |
115 offset = 0; | |
116 cur_line += pitch * row_height; | |
117 } | |
118 } | |
2275
e27ab5f4bfe3
Fix off by one in oscilloscope rendering
Michael Pavone <pavone@retrodev.com>
parents:
2245
diff
changeset
|
119 |
e27ab5f4bfe3
Fix off by one in oscilloscope rendering
Michael Pavone <pavone@retrodev.com>
parents:
2245
diff
changeset
|
120 |
2245 | 121 render_framebuffer_updated(scope->window, WIDTH); |
122 } | |
123 | |
124 void scope_close(oscilloscope *scope) | |
125 { | |
126 render_destroy_window(scope->window); | |
127 for (uint8_t i = 0; i < scope->num_channels; i++) | |
128 { | |
129 free(scope->channels[i].samples); | |
130 } | |
131 free(scope->channels); | |
132 free(scope); | |
133 } |