Diogo Costa
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    /** * DISCLAIMER * This is a solution written from memory after the test, before even knowing my grade (altough I passed every public test in the exam). * There may be a mistake in my code or reasoning that I did not catch at the time. Moreover, I may be misremembering or mistyping something * so don't be surprised if this doesn't even compile. If you spot any mistake let me know (you may add comments to this file on google drive). * * In short, NOTHING IN THIS FILE IS GUARANTEED TO BE CORRECT. * * This is an mix between what I recall from my solution, some extra checks (like calling free to avoid memory leaks) that probably won't even * be evaluated by the LCF and changes that I thought would make my solution easier to understand. There are also comments in some parts explaining * why I did what I did or some LCF issues I stumbled upon. */ static uint16_t x_res, y_res; static uint8_t bpp, bypp, r_pos, r_sz, b_pos, b_sz, g_pos, g_sz; static size_t video_mem_size; static phys_addr_t phys_addr; static uint8_t *video_mem; static uint8_t *first_square_back_buffer; static uint8_t *second_square_back_buffer; #define BIT(n) (1<<(n)) #define VERT_RETR_INT BIT(7) #define CONTROL_REG 0x28 #define IRQ_LINE 7 #define FPS 30 int util_sys_inb(int port, uint8_t *byte) { uint32_t val; if (sys_inb(port, &val)) return 1; *byte = val; return 0; } int draw_pixel(uint8_t *buf, uint16_t x, uint16_t y, uint32_t color) { if (x >= x_res || y >= y_res) return 0; // it's not an error to be out of bounds, but nothing should be drawn uint8_t *mem_pos = buf + (y*x_res + x)*bypp; color &= 0x00ffffff; // I needed to ignore the most significant bits to pass the tests in mode 2 if (memcpy(mem_pos, &color, bypp) == NULL) return 1; return 0; } int draw_hline(uint8_t *buf, uint16_t x, uint16_t y, uint16_t len, uint32_t color) { for (int i = 0; i < len; i++) { if (draw_pixel(buf, x+i, y, color)) return 1; } return 0; } int draw_square(uint8_t *buf, uint16_t x, uint16_t y, uint16_t len, uint32_t color) { for (int i = 0; i < len; i++) { if (draw_hline(buf, x, y+i, color)) return 1; } return 0; } int part1(uint16_t x, uint16_t y, uint16_t len, uint32_t color, uint8_t delay) { if (memcpy(video_mem, first_square_back_buffer, video_mem_size) == NULL) return 1; sleep(delay); return 0; } static int hook_id = 1; int part2(uint16_t x, uint16_t y, uint16_t len, uint32_t color, uint8_t delay) { int ipc_status, irq_set = BIT(hook_id); message msg; bool fail = false; if (sys_irqsetpolicy(IRQ_LINE, IRQ_REENABLE, &hook_id)) return 1; uint8_t ticks = delay * FPS * 2; while( ticks > 0 ) { /* Get a request message. */ int r; if ( (r = driver_receive(ANY, &msg, &ipc_status)) != 0) { printf("driver_receive failed with: %d", r); continue; } if (is_ipc_notify(ipc_status)) { /* received notification */ switch (_ENDPOINT_P(msg.m_source)) { case HARDWARE: /* hardware interrupt notification */ if (msg.m_notify.interrupts & irq_set) { if (ticks > delay * FPS) { if (memcpy(video_mem, first_square_back_buffer, video_mem_size) == NULL) { fail = true; break; } } else { if (memcpy(video_mem, second_square_back_buffer, video_mem_size) == NULL) { fail = true; break; } } ticks--; } break; default: break; } } else { /* no standard messages expected: do nothing */ } } if (sys_irqrmpolicy(&hook_id)) return 1; return fail; } int test_draw_square(uint32_t mode, uint16_t x, uint16_t y, uint16_t len, uint32_t color, uint8_t delay) { mode_info_t inf; if (get_mode_info(mode, &inf)) return 1; x_res = inf.x_res; y_res = inf.y_res; bpp = inf.bpp; bypp = (bpp%8 == 0)? (bpp/8) : (bpp/8 + 1); r_pos = inf.r_pos; r_sz = inf.r_sz; g_pos = inf.g_pos; g_sz = inf.g_sz; b_pos = inf.b_pos; b_sz = inf.b_sz; phys_addr = inf.phys_addr; video_mem_size = x_res * y_res * bypp; video_mem = map_phys_mem(phys_addr, video_mem_size); // The next two lines are not mandatory, but are used for this solution. An explanation can be found below first_square_back_buffer = malloc(video_mem_size); second_square_back_buffer = malloc(video_mem_size); if (video_mem == NULL || first_square_back_buffer == NULL || second_square_back_buffer == NULL) { free(first_square_back_buffer); free(second_square_back_buffer); return 1; } uint8_t cmd; if (util_sys_inb(CONTROL_REG, &cmd)) { // Just in case, so that the unknown bytes are not modified free(first_square_back_buffer); free(second_square_back_buffer); return 1; } cmd &= 0xf0; cmd |= mode; /* * For modularity I initially didn't have the following line. I would only set VERT_RETR_INT in part2, * calling util_sys_inb again and updating the control word. This would result in the test not passing. * * Although that solution would be correct, the LCF is not smart enough to detect it as valid, because it * was not prepared to handle multiple writes to the control word. */ cmd |= VERT_RETR_INT; // Comment this line if you wish to go with part 1 if (sys_outb(CONTROL_REG, cmd)) { free(first_square_back_buffer); free(second_square_back_buffer); return 1; } /* * There are many ways to implement this. For the part 1 you could just draw directly into the video_mem. * * For the second part, as you need to erase the first square before drawing the second one you could draw a black * square before drawing the second square. This would cause flickering on my machine (although it passes the public tests, so I'm not sure * if LCF is even able to spot that problem) which could be fixed if the square was only erased in the transition frame instead of every frame. * * However I think a solution with back buffers is simpler and less error prone (maybe you don't share the same opinion though). Basically, as there are * only two static images (squares), I drew each in a separate auxiliary frame buffer and used memcpy to show the frame I wanted. */ if (draw_square(first_square_back_buffer, x, y, len, color)) { free(first_square_back_buffer); free(second_square_back_buffer); return 1; } if (draw_square(second_square_back_buffer, y, x, len, color)) { // You can ignore this one for part 1. free(first_square_back_buffer); free(second_square_back_buffer); return 1; } int vr; // Part 1 //vr = part1(x, y, len, color, delay); // Part 2. vr = part2(x, y, len, color, delay); free(first_square_back_buffer); free(second_square_back_buffer); cmd &= BIT(4) | BIT(5) | BIT(6); if (sys_outb(CONTROL_REG, cmd)) return 1; return vr; }

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully