{"id":257,"date":"2016-05-27T22:51:42","date_gmt":"2016-05-27T20:51:42","guid":{"rendered":"http:\/\/tos.acoustic-velocity.com\/?p=257"},"modified":"2021-10-24T12:05:27","modified_gmt":"2021-10-24T10:05:27","slug":"tasks-schedules-and-priorities","status":"publish","type":"post","link":"https:\/\/acoustic-velocity.com\/?p=257","title":{"rendered":"Tasks, schedules and priorities"},"content":{"rendered":"<p>Here we are with another rather technical update! Last time I was posting some information about how I want to get more diversity into the game graphics by switching parts of the character set on the fly while the player moves through the world. That actually raised an interesting new problem: how to handle tasks with different priorities!<\/p>\n<p>Up to now there were only two possible priorities: tasks that need to be done during the currently displayed frame (like color RAM scrolling or\u00a0inserting new characters at the border of the screen) and tasks that only need to be finished\u00a0eventually during the next frames (like scrolling the\u00a0currently invisible double\u00a0buffer or\u00a0checking for trigger areas for events).\u00a0The character set swapping\u00a0adds another priority: in theory it can take quite long and there is no need to have it finished in the next 2-3 frames, it is ok even if it takes e.g. 10 frames or so.\u00a0Instead of patching this into the existing engine I decided to\u00a0do it right from the start\u00a0and be\u00a0prepared for any upcoming additional tasks.\u00a0\u00a0Which means: implement a task scheduler!<\/p>\n<p><a href=\"https:\/\/acoustic-velocity.com\/wp-content\/uploads\/2016\/05\/clocks-1098080_640.jpg\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-258\" src=\"https:\/\/acoustic-velocity.com\/wp-content\/uploads\/2016\/05\/clocks-1098080_640.jpg\" alt=\"clocks-1098080_640\" width=\"640\" height=\"360\" srcset=\"https:\/\/acoustic-velocity.com\/wp-content\/uploads\/2016\/05\/clocks-1098080_640.jpg 640w, https:\/\/acoustic-velocity.com\/wp-content\/uploads\/2016\/05\/clocks-1098080_640-300x169.jpg 300w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/a><\/p>\n<p>What does a task scheduler do? It makes sure, that most of the processor time is spent on the tasks with the highest priority. I simplified the concept a bit,\u00a0so that higher priority tasks are always finished before lower priority tasks are continued. The implementation is actually simple, although there is some mean stack fiddling involved and it needed a bit of head scratching until I got it right. The interface of the scheduler module has only two routines: <em>task_add<\/em>, which adds a new task with a given priority and\u00a0might switch to it immediately (if it is the highest priority), and <em>task_done<\/em>, which is called by a task when it is done and selects the next highest priority task to be executed.\u00a0\u00a0All that is needed is to modify the stack such, that the <a href=\"https:\/\/www.c64-wiki.com\/index.php\/RTI\">RTI <\/a>instruction at the end of the interrupt routine jumps to the right task. Apologies for the clumsy block diagram trying to depict this\u00a0with an example. Did I mention that I am not a graphician?<\/p>\n<p>In any case: implementation is done and unit tests are passing (yay!), so I am looking forward to put this new module into good use!<\/p>\n<p><a href=\"https:\/\/acoustic-velocity.com\/wp-content\/uploads\/2016\/05\/scheduler-1.png\"><img decoding=\"async\" loading=\"lazy\" class=\"wp-image-262 size-full aligncenter\" src=\"https:\/\/acoustic-velocity.com\/wp-content\/uploads\/2016\/05\/scheduler-1.png\" width=\"465\" height=\"641\" srcset=\"https:\/\/acoustic-velocity.com\/wp-content\/uploads\/2016\/05\/scheduler-1.png 465w, https:\/\/acoustic-velocity.com\/wp-content\/uploads\/2016\/05\/scheduler-1-218x300.png 218w\" sizes=\"(max-width: 465px) 100vw, 465px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here we are with another rather technical update! Last time I was posting some information about how I want to get more diversity into the game graphics by switching parts of the character set on the fly while the player moves through the world. That actually raised an interesting new problem: how to handle tasks &hellip; <a href=\"https:\/\/acoustic-velocity.com\/?p=257\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Tasks, schedules and priorities<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/acoustic-velocity.com\/index.php?rest_route=\/wp\/v2\/posts\/257"}],"collection":[{"href":"https:\/\/acoustic-velocity.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/acoustic-velocity.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/acoustic-velocity.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/acoustic-velocity.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=257"}],"version-history":[{"count":8,"href":"https:\/\/acoustic-velocity.com\/index.php?rest_route=\/wp\/v2\/posts\/257\/revisions"}],"predecessor-version":[{"id":268,"href":"https:\/\/acoustic-velocity.com\/index.php?rest_route=\/wp\/v2\/posts\/257\/revisions\/268"}],"wp:attachment":[{"href":"https:\/\/acoustic-velocity.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=257"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/acoustic-velocity.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=257"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/acoustic-velocity.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=257"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}