larsbrinkhoff / terminal-simulator

Simulation of VT52 and VT100 terminal hardware.
GNU General Public License v3.0
113 stars 17 forks source link

Turn sound off option #51

Closed Anonymous3-a closed 2 months ago

Anonymous3-a commented 2 months ago

On my computer, sound does not work (probably an issue with the newness of it, please don't suggest fixing my sound). Is there a way to mute the sound for the vt100? If I launch it, it says ALSA lib pcm_dmix.c:1000:(snd_pcm_dmix_open) unable to open slave and crashes. I'll try to poke around in the code for now, but I don't know C very well.

Anonymous3-a commented 2 months ago

Ok, just found out sound is needed for syncing between simulated time and real time. I'll see about at least trying to fix that.

drforbin commented 2 months ago

The sound , sounds terrible on my system. Anyway to turn it off or. pulse audio and this sim donot play nice together.

Anonymous3-a commented 2 months ago

I pretty much just gave up and removed sound and put for(int i=0;i<25000;i++){} in the main.c cputhread function, like so:

static int cputhread (void *arg)
{
  unsigned long long cycles, previous = get_cycles ();
  memcpy (memory, vt100rom, 0x2000);
  for (;;) {
    execute ();
    cycles = get_cycles ();
    events (cycles - previous);
    previous = cycles;
    for(int i=0;i<25000;i++){} // Probably not a great way to fix it
  }
  return 0;
}

It may be maxing out a cpu core, but for some reason I can't find a better way. If anyone can help, that would be great. (There's gotta be a better way to frame limit than sound?)

drforbin commented 2 months ago

ok...how do I remove sound?

Anonymous3-a commented 2 months ago

in sound.c change

  dev = SDL_OpenAudioDevice(NULL, 0, &want, &have, SDL_AUDIO_ALLOW_FORMAT_CHANGE);
  if (dev == 0) {
    LOG (SND, "Could not open an audio device.");
    LOG (SND, "For now, audio is needed to synchronize simulated time against real time.");
    exit (1);                                                                                                                                                                                                                                                                             
  }

Remove the exit (1); like so:

  dev = SDL_OpenAudioDevice(NULL, 0, &want, &have, SDL_AUDIO_ALLOW_FORMAT_CHANGE);
  if (dev == 0) {
    LOG (SND, "Could not open an audio device.");
    LOG (SND, "For now, audio is needed to synchronize simulated time against real time.");
    //exit (1);                                                                                                                                                                                                                                                                             
  }
drforbin commented 2 months ago

What about use a sleep?

drforbin commented 2 months ago

sleep is too long...try usleep milliseconds

Anonymous3-a commented 2 months ago

I tried usleep but it was too slow for some reason (if you know how, please do help, I don't like busy sleep).

drforbin commented 2 months ago

Yeah..It's very poor design. Your issue was not getting a audio device. Mine is it's driving pulse audio crazy.

So let's see. I think i's clocking it at 48Khz so 1/48khz=2.08e-5 about .02ms

I guess it is too slow.

need micro 2nd sleep

drforbin commented 2 months ago

how about nanosleep()?

Anonymous3-a commented 2 months ago

I tried nanosleep, but since I'm not that good at C (I only know what overlaps between C and C++, and not even all of that) I couldn't get it to work. Do you know any C? I might try again later.

drforbin commented 2 months ago

Yeah. I'm very good at C. That's my main lang. What happened? Did you use the proper data structures?

Anonymous3-a commented 2 months ago

I'm not fully sure, really. I'm better with C++, which is close, but I can't write a fully working program in C without a lot of help. I'll try again.

Anonymous3-a commented 2 months ago

So, I got it working, but it's too slow. I used nanosleep((const struct timespec[]){{0, <nanoseconds>L}}, NULL); (replacing <nanoseconds> with the amount of nanoseconds`, but no matter how low I made the number, it's just too slow. Any ideas?

Anonymous3-a commented 2 months ago

Update (see https://blog.bearcats.nl/accurate-sleep-function/ ): It's not going to work very well. You need a while (time not up) { nop; }-like code to make it work accurately. I'll try to get that working more precisely.

drforbin commented 2 months ago

Ok...I'll see if i can code it later.

drforbin commented 2 months ago

Something has to be done! Using pulse audio is terrible for this.

drforbin commented 2 months ago

PS....how did lars code the vt52 etc? did doesn't seem to have this problem

Anonymous3-a commented 2 months ago

vt52 doesn't have a sound.c? Does it even have sound? I know that it's not giving any sound related errors...

drforbin commented 2 months ago

No sound. but wasn't the sound also used for time keeping?

Anonymous3-a commented 2 months ago

Hmm, I'll investigate that.

Anonymous3-a commented 2 months ago

I noticed that in vt100, throttle() isn't used, and noticed that vt52 seems to keep frame rate by going

if ((cycles % 1000000) == 0)
  throttle ()

so I tried adding

    if ((cycles % 10000) == 0)
      throttle();

to vt100 main.c cputhread(), and it did limit the frame rate. I still need to figure out how big/small to make the number to get it to run smoothly.
Update: 32 seems to be a good number.

drforbin commented 2 months ago

throttle links back with sound doesn't it?

Anonymous3-a commented 2 months ago

I'm not fully sure, but at first glance, it doesn't seem to. Try adding

if ((cycles % 32) == 0)
  throttle()

to cputhread(), it works for me (and maybe try other numbers, I don't have a vt100 to compare speeds)

drforbin commented 2 months ago

Ill have to look into where that function 'i.e. throttle() is defined

Anonymous3-a commented 2 months ago

throttle() is defined two functions above cputhread.

drforbin commented 2 months ago

thxs..i'll have a look

drforbin commented 2 months ago

I will look tonight. any progress?

Anonymous3-a commented 2 months ago

Not yet, I just woke up. I'll get back to it though.

Anonymous3-a commented 2 months ago

So, get this, throttle() pretty much just sleeps. How did I not notice SDL_Delay()? Turns out the whole needing sound thing could have been easily averted with throttling. We might need to ping someone who knows the code better, but I haven't seen any side effects so far, except that (and I'm not sure if this is a side effect, I don't have a vt100 to test against) if I send too many lines at once, it starts displaying weird and cut off chars (try it, navigate to a big file and less or cat it). I tested with Telehack, and set baud to 4800, and it displayed very well (except when I send too many lines at once), and 2400 worked better too.

drforbin commented 2 months ago

Well it's calling SDL for the sleep, So I think SDL is filling in for the sleep? What you think?

Anonymous3-a commented 2 months ago

Yeah, SDL_Delay is just sleeping. I checked htop, and it isn't taking up an entire CPU core anymore.

drforbin commented 2 months ago

I'll look up the SDL_delay function when I get back. It's probably using some hardware/software implementation which creates a delay. I doubt it's solely software but is mostly likely based on some clock within the sound hard.

He's using this as a high quality sleep I think. For me the problem was the audio. Unlike you where your audio open failed (???are you using pulse?) mine did get a handle returned, but the constant updating cause the audio to glitch out. Maybe we can add a switch to disable all this. I currently have it running with no audio and LOW CPU use.

Thoughts?

Anonymous3-a commented 2 months ago

I am looking at the source code for SDL, and found:

void SDL_Delay(Uint32 ms)
{
    SDL_DelayNS(SDL_MS_TO_NS(ms));
}

SDL_DelayNS is defined in folders for each platform, and I think that it loads the correct one for the platform it runs on. The implementation for unix is a bit long to paste, but it basically just uses nanosleep (or emscripten sleep, if using emscripten).

Edit: For reference, here is the timer section of SDL source code: https://github.com/libsdl-org/SDL/tree/main/src/timer

drforbin commented 2 months ago

humm, I was sure it would be more hardware based. Almost like the interrupt clock kernel(s) use for scheduling etc

larsbrinkhoff commented 2 months ago

Hello everyone. I was away, but now I'm back and can comment. It seems you have hashed out most things already, and I can confirm your theories. You should be able to remove audio and call throttle instead. And yes, throttle calls SDL_Delay to sleep. It could have used e.g. nanosleep instead, but since I'm using SDL API's already I figured this way it would be a little more portable.

Anonymous3-a commented 2 months ago

Great, thanks. That should be enough and I'll close the issue, although if you have a specific way to make it run at a normal speed, that would be nice.

drforbin commented 2 months ago

It doesn't run at normal speed? I'd like to add a switch to take out the sound when desired

Anonymous3-a commented 2 months ago

I meant that I don't know what "normal speed" is, and would like to know how to get it more accurate.

If you want to add a switch, you'd need to modify the switch case at line 165 of main.c and the help text at line 127 of main.c. Not sure about disabling sound. sys.c seems promising.

I might try adding the sound switch later, but I wouldn't know if sound was disabled (my sound doesn't work either way, I'm trying to fix it).

drforbin commented 2 months ago

normal speed seems to be around modulus 20... As for disabling sound I alrdy have it disabled. Just got to code it

drforbin commented 2 months ago

Do you think I should just have a switch to turn of sound code at command line? Also if the 20 modulus is not used or a bit below the screen tears when scrolling.

Anonymous3-a commented 2 months ago

Thanks, I'll change to mod 20.

By the way, sound.c has a section to set frequency. I remember on my previous computer with sound, tweaking that value can help with sound quality, if you want to try it.