Commits

Cliff Biffle committed d47fc63

Reworked RTC-set syscall to update day-of-year, which is required for epoch calculation.

  • Participants
  • Parent commits d36a676

Comments (0)

Files changed (1)

File kernal/kernal.s

 
           ldr r0, [r12, #36]  @ Load calendar date from user's stack frame.
 
-          ubfx r2, r0, #0, #8 @ Extract day-of-month into r2.
-          str r2, [r1, #LPC_RTCTCG_to_DOM]
+          ubfx r2, r0, #16, #16 @ Extract year into r2.
+          tst r2, #0b11       @ Set Z for leap year.
+          sub r2, #2000       @ Correct Gregorian year to epoch year.
+          str r2, [r1, #LPC_RTCTCG_to_YEAR]
 
           ubfx r2, r0, #8, #8 @ Extract month into r2.
           str r2, [r1, #LPC_RTCTCG_to_MONTH]
 
-          ubfx r2, r0, #16, #16 @ Extract year into r2.
-          str r2, [r1, #LPC_RTCTCG_to_YEAR]
+          @ Calculate day-of-year using month number in r2.
+          adr r3, _month_to_doy_table
+          ldrh r3, [r3, r2, LSL #1]     @ r3 = days before this month
+          bne 1f              @ Use Z flag set above to skip ahead for non-leap.
 
-          @ TODO: calculate DOY.
+          cmp r2, #2          @ Are we past February?
+          it hs               @ If yes,
+          addhs r3, #1        @ Add a day.
+
+1:        ubfx r2, r0, #0, #8 @ Extract day-of-month into r2.
+          str r2, [r1, #LPC_RTCTCG_to_DOM] @ While we've got it, set it.
+
+          adds r3, r2         @ Add days this month to DOY.
+          str r3, [r1, #LPC_RTCTCG_to_DOY]
 
           bx LR
 
+@ Lookup table from month (0-11) to its initial day-of-year (0-365)
+_month_to_doy_table:
+          .short 1   @ January
+          .short 32  @ February
+          .short 60  @ March
+          .short 91  @ April
+          .short 121 @ May
+          .short 152 @ June
+          .short 182 @ July
+          .short 213 @ August
+          .short 244 @ September
+          .short 274 @ October
+          .short 305 @ November
+          .short 335 @ December
+
 k_system_exit_mon:
           b mon_exit